I have following code that creates a field for password.
// 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');
In my controller I need to remove the validators and make 'required' false from the controller depending upon some conditions.
For example:-
if($someCondition){
//Set required to false and remove validator here somehow
}
Does any one know a solution for this case?
If you have instantiated your form in the controller like this:-
$loginForm = new Application_Form_LoginForm();
Then you can set the attributes for the Password (or any other) element like this:-
if($someCondition){
$loginForm->Password->setRequired(false);
$loginForm->Password->setValidators(array());
}
Or, as Zend_Form_Element::setRequired() returns an instance of Zend_Form_Element, you can do this:-
if($someCondition){
$loginForm->Password->setRequired(false)->setValidators(array());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With