php - Custom Validator Issue in Zend Framework -
php - Custom Validator Issue in Zend Framework -
i have form called loginform, extends recipeform in turn extends zend_form. recipefrom returns decorators.
when form submitted, next error, 'message: method addvalidator not exist'.
class recipe_form_loginform extends recipe_form_recipeform { public function init() { parent::init(); $this->setname('loginform') ->setaction('/login'); // add together email element $email = $this->addelement('text', 'email', array( 'label' => 'email addresses', 'required'=> true, 'size'=>12, 'filters'=>array('stringtrim'), 'decorators' => $this->getelementdecorator('email'), )); $email->addvalidator(new recipe_validate_emailaddress(), true, array( 'messages' => array( recipe_validate_emailaddress::invalid => 'please come in email in right format', recipe_validate_emailaddress::emailisempty => 'please come in email address' ))); } class recipe_validate_emailaddress extends zend_validate_abstract { const invalid = 'notvalid'; const emailisempty = 'isempty'; protected $_messagetemplates = array( self::invalid => "email in invalid format", self::emailisempty => "you have fill email field" ); public function isvalid($value){ $response = parent::isvalid($value); if(!$response){ $this->_message = array(self::invalid => "please come in valid email address"); } homecoming $response; } } ?>
when phone call $this->addelement()
within zend_form object, returns form object itself, rather element created.
you can create 1 of next changes:
$this->addelement('text', 'email', ...); $email = $this->getelement('email'); $email->addvalidator(...); // or $email = new zend_form_element_text('email'); $email->addvalidator(...) ->setlabel(...) ->setrequired(...); $this->addelement($email);
to set error message, think should instead of setting $this->_message.
$this->_error(self::invalid);
since looks class extending zend's email validator override message, can override zend's messages , not need extend class. taken validator in 1 of projects, ignore stuff , pay attending messages emailaddress validator.
$this->addelement('text', 'email', array( 'label' => 'email address:', 'required' => false, 'filters' => array('stringtrim', 'stringtolower'), 'validators' => array( array('emailaddress', true, array( 'messages' => array( zend_validate_emailaddress::invalid_format => "'%value%' not valid email address. example: you@yourdomain.com", zend_validate_emailaddress::invalid_hostname => "'%hostname%' not valid hostname email address '%value%'" ) )), array('db_recordexists', true, array( 'table' => 'accounts', 'field' => 'email', 'messages' => array( zend_validate_db_recordexists::error_no_record_found => "no business relationship email address found" ) )) ), 'decorators' => $this->elementdecorators ));
php zend-form zend-validate
Comments
Post a Comment