Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

Using Custom Validator in Zend Framework

In Zend_Form i want to validate for duplicate records that might exist in database. since i am using doctrine 1.2.4 i am using custom validator.

i defined a custom validator class Application_Validator_NoRecordExists extends Zend_Validate_Abstract which sits in application/validator directory.

the class definition is taken from

Zend_Validate: Db_NoRecordExists with Doctrine

what i am confused about is how to use the validator in my forms for example i want to validate the email address to check wether duplicate record exist with the custom validator class.

class Application_Form_User extends Zend_Form
{
    public function init()
    {
        $this->setAction('/admin/user/create/')
             ->setMethod('post');

        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('Email address:')
              ->setOptions(array('size' => 50))
              ->setRequired(true)
              ->addValidator('EmailAddress', true)
              ->addFilter('HTMLEntities')
              ->addFilter('StringToLower')
              ->addFilter('StringTrim');

        //add element to form
    }
}

how do i do it?

Upvotes: 1

Views: 1665

Answers (1)

dbrumann
dbrumann

Reputation: 17166

You have to add a prefix path to your form, in order for your class to be recognized.

Zend_Form::addPrefixPath(string $prefix, string $path, string $type = null)

The following passage from the reference manual should make it clear: http://framework.zend.com/manual/1.11/en/zend.form.elements.html#zend.form.elements.validators

Upvotes: 2

Related Questions