向 Zend 框架中的表单元素添加正则表达式验证器 [英] Add a regex validator to a form element in the Zend Framework
问题描述
我在 zend Framework 中创建了一个 Form 类.
I created a Form class in zend Framework.
类 Application_Form_UserSignup 扩展 Zend_Form{
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 : < >'))
));
}
}
我想用我自己的正则表达式使用 Zend_Validate_Regex 验证器来验证它.
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.
错误是:
消息:传递给 addValidators() 的验证器无效堆栈跟踪:
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 {主要}
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}
任何帮助将不胜感激!
推荐答案
它的 addValidatorS(多个验证器):
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 : < >'))
)
));
这篇关于向 Zend 框架中的表单元素添加正则表达式验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!