mcfly's
mcfly's

Reputation: 123

How to disable filter of a field in Zend Framework 2 in the controller?

Form to add/edit user I get from service manager with already installed filter, which is the test password. But this password is not needed when the user is edited. Can I somehow disabled password field validation in the controller?

In the getServiceConfig function of the 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;
},
// ....

In the controller I first of all getting a form object from service manager:

$form = $this->getServiceLocator()->get('UserCRUDForm');

Then disable user password validation and requirements, when user is edited and password not specified:

if ($user_id > 0 && $this->request->getPost('password') == '') {
      $form->.... // Someway gained access to the filter class and change the password field validation
}

And after this i make a validation:

$form->isValid();

Upvotes: 3

Views: 3022

Answers (4)

Kārlis Millers
Kārlis Millers

Reputation: 674

$inputFilter->add(array(
        'name'        => 'password',
        'required'    => true,
        'allow_empty' => true,
    ));

And on ModeleTable: saveModule:

public function saveAlbum(Album $album)
    {
        $data = array(
            'name'        => $album->name,
        );
        if (isset($album->password)){
            $data['password'] = $album->password;
        }

Upvotes: 0

mcfly's
mcfly's

Reputation: 123

I found it!

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

This lines is disables requirement of input form fields :)

Upvotes: 6

ins0
ins0

Reputation: 3928

if you like to set all validators by yourself, call inside your form class

$this->setUseInputFilterDefaults(false);

to disable auto element validations/filter added from zend.

if you like to remove filter from elements call in your controller after your form object this

$form->getInputFilter()->remove('InputFilterName');

Upvotes: 3

cptnk
cptnk

Reputation: 2420

$form->get('password')->removeValidator('VALIDATOR_NAME'); should do the trick.

Note that you may have to iterate trough the Validatorchain when using Fieldsets.

Upvotes: 0

Related Questions