A
A
Andrey2014-07-31 16:02:00
Zend Framework
Andrey, 2014-07-31 16:02:00

How to disable single form field filter in Zend Framework 2 in controller?

Such situation.
I get the form for adding/editing a user from the service manager with an already installed filter, in which there is a password check. But this password is not needed when the user is being edited. Can I somehow disable field validation in the form filter in the controller?
In getServiceConfig module:

// ....
'UserCRUDFilter' => function($sm)
{
    return new \Users\Form\UserCRUDFilter();
},

'UserCRUDForm' => function($sm, $param, $param1)
{
    $form = new \Users\Form\UserCRUDForm();
    $form->setInputFilter($sm->get('UserCRUDFilter'));
    return $form;
},
// ....

That is, first I get the form
$form = $this->getServiceLocator()->get('UserCRUDForm');

Then I turn off the need to enter a password if the user is being edited and the password is not entered
if ($user_id > 0 && $this->request->getPost('password') == '') {
      $form->.... // Как-то получаю доступ к классу фильтра и убираю required у проверки пароля
}

And then I do
$form->isValid();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2014-07-31
@mcflys

Found a solution. Enjoy your health :)

// If user is editted - clear password requirement
if ($user_id > 0) {
    $form->getInputFilter()->get('password')->setRequired(false);
    $form->getInputFilter()->get('confirm_password')->setRequired(false);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question