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:


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?


Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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');
if ($form->isValid()) {

Solution 2

The first thing I thought about was to remove the validator from your myElement’s ValidatorChain. You could get it within the controller with:

$form->getInputFilter()->get( 'myElement' )->getValidatorChain()

It seems like you can’t remove from the ValidatorChain, just add. Check this post. Matthew Weier O’Phinney, from Zend, explains why it can’t be done and a possible solution for your scenario.

The way I solve this problem, is checking the ‘remove condition’ when I create the validator in the FormFilter class. If you use annotations I think it doesn’t works for you, so Matthew suggestions is the one you should use.

Or you could try the one in this post from @Stoyan Dimov: define two forms, a kind of BasicForm and ExtendedForm. The first one have all the common form elements, the second one is an extended one of the other with the rest of fields. Depending on your condition you could use one or another.

Solution 3

In class ValidatorChain implements Countable, ValidatorInterface, add a new method:

public function remove($name){
    foreach ($this->validators as $key => $element) {
        $validator = $element['instance'];
        if($validator instanceof $name){

Use like this:


Solution 4

There must be a validator defined for this particular element that you are trying to remove.

In your controller where you are adding new elements to form, there must be addValidator calling like:


That is actually causing validation to be failed. So you have removed the element from form but you still have validation defined on that element to be checked.

If you are not able to find this validation adding function in controller, try to see if it has been defined through config file.

You can read further about form validation in zf here: http://framework.zend.com/manual/1.12/en/zend.form.elements.html

Solution 5

I remove the element with:


When I post the form I disable the validator on this element with :


Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply