Add a regex validator to a form element in the Zend Framework
I created a Form class in zend Framework.
class Application_Form_UserSignup extends Zend_Form {
public function init()
{
// Set the method for the display form to POST
$this->setMethod('post');
// Add an Firstname element
$this->addElement('text', 'firstname', array(
'label' => 'Your first name:',
'required' => true,
'validators' => array('regex', false, array(
'pattern' => '/[^<>]/i',
'messages' => 'Your first name cannot contain those characters : < >'))
));
}
}
I would like 开发者_如何转开发to validate it with my own regex using the Zend_Validate_Regex validator.
There must be an error in the syntax because I get this error but I cannot figure it out.
The error is :
Message: Invalid validator passed to addValidators() Stack trace:
0 /usr/share/php/libzend-framework-php/Zend/Form/Element.php(1217): Zend_Form_Element->addValidators(Array)
1 /usr/share/php/libzend-framework-php/Zend/Form/Element.php(363): Zend_Form_Element->setValidators(Array) 2 /usr/share/php/libzend-framework-php/Zend/Form/Element.php(253): Zend_Form_Element->setOptions(Array) 3 /usr/share/php/libzend-framework-php/Zend/Form.php(1108): Zend_Form_Element->__construct('firstname', Array) 4 /usr/share/php/libzend-framework-php/Zend/Form.php(1039): Zend_Form->createElement('text', 'firstname', Array) 5 /home/damiens/workspace/manu/application/forms/UserSignup.php(18): Zend_Form->addElement('text', 'firstname', Array) 6 /usr/share/php/libzend-framework-php/Zend/Form.php(240): Application_Form_UserSignup->init() 7 /home/damiens/workspace/manu/application/controllers/UsersController.php(35): Zend_Form->__construct() 8 /usr/share/php/libzend-framework-php/Zend/Controller/Action.php(513): UsersController->signupAction() 9 /usr/share/php/libzend-framework-php/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('signupAction') 10 /usr/share/php/libzend-framework-php/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) 11 /usr/share/php/libzend-framework-php/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch() 12 /usr/share/php/libzend-framework-php/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run() 13 /home/damiens/workspace/manu/public/index.php(26): Zend_Application->run() 14 {main}
Any Help would be appreciated !
Its addValidatorS (multiple validators):
$this->addElement('text', 'firstname', array(
'label' => 'Your first name:',
'required' => true,
'validators' => array(
array('regex', false, array(
'pattern' => '/[^<>]/i',
'messages' => 'Your first name cannot contain those characters : < >'))
)
));
精彩评论