Search code examples
phpvalidationzend-framework2zend-formzend-form-element

Zend Framework 2 - Removed form element causes validation to fail


I use a certain form in several places. In one of them I need to ignore a form element which I set programmatically after the validation.

Because it's just an exception I don't want to create a new form. So I thought, I just remove this element in the controller like:

$myForm->remove('myElement');

The problem is that the form now won't validate. I don't get any errors but the $myForm->isValid() just returns an empty value.

Any ideas what I might be doing wrong?


Solution

  • Ok, finally I found a solution! You can define a ValidationGroup which allows you to set the attributes you'd like to validate. The others are not validated:

    $form->setValidationGroup('name', 'email', 'subject', 'message');
    $form->setData($data);
    if ($form->isValid()) {
        ...