我有以下代码创建密码字段。
// Element: password
$this->addElement('Password', 'password', array(
'label' => 'Password',
'description' => 'Passwords must be at least 6 characters long.',
'required' => true,
'allowEmpty' => false,
validators' => array(
array('NotEmpty', true),
array('StringLength', false, array(6, 32)),
)
));
$this->password->getDecorator('Description')->setOptions(array('placement' => 'APPEND'));
$this->password->getValidator('NotEmpty')->setMessage('Please enter a valid password.', 'isEmpty');
在我的 Controller 中,我需要删除验证器并根据某些条件从 Controller 中将“required”设置为 false。
例如:-
if($someCondition){
//Set required to false and remove validator here somehow
}
有人知道这种情况的解决方案吗?
最佳答案
如果您在 Controller 中像这样实例化了表单:-
$loginForm = new Application_Form_LoginForm();
然后您可以像这样设置密码(或任何其他)元素的属性:-
if($someCondition){
$loginForm->Password->setRequired(false);
$loginForm->Password->setValidators(array());
}
或者,当 Zend_Form_Element::setRequired()
返回 Zend_Form_Element 的实例时,您可以这样做:-
if($someCondition){
$loginForm->Password->setRequired(false)->setValidators(array());
}
关于zend-framework - 移除验证器并在 Controller 中将 required 设置为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12853748/