简体   繁体   中英

Custom Validator not working in zend

so I wrote a custom date validater called Date and when I try and use it, zend falls back to the zend_validate_date call:

    $datePosted = new Zend_Form_Element_Text('datePosted');
    $datePosted->setLabel('Date Job Was Posted?: ')
            ->setRequired(true)
            ->addFilter('stripTags')
            ->addFilter('stringTrim')
            ->addPrefixPath('Hg2_Validate_Date', 'Hg2/Validate/', 'validate')
            ->addValidators(array(
                array(
                    'validator' => 'NotEmpty',
                    'breackChainOnFailure' => true
                ),
                array(
                    'validator' => 'stringLength',
                    'options' => array(0, 10)
                ),
                array(
                    'validator' => 'Date'
                ),
                array(
                    'validator' => 'alnum',
                    'options' => array('allowWhiteSpaces' => true)
                )
            ));

    return $datePosted;

This is what I have and it should use my date and not zends date no?

the custom validation can be seen by the addPrefixPath() where I add my custom validator to this element. My understanding is that now if I call Date or date bellow in the 'validator' it will call my date and not zends?

As a prefix you don't have to add the _Date to the path. If you look at the documentation

addPrefixPath($prefix, $path, $type = null)
adds a prefix/path association to the loader specified by $type.

With your current prefix argument this will probably extend to a class named Hg2_Validate_Date_Date which it will not find.

So this should work for you

->addPrefixPath('Hg2_Validate', 'Hg2/Validate/', 'validate')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM