Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove validators and set required as false in controller

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?

like image 436
Gokul Shinde Avatar asked Jun 21 '26 08:06

Gokul Shinde


1 Answers

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());
}
like image 59
vascowhite Avatar answered Jun 23 '26 04:06

vascowhite