The mystery of missing Form->getData() values in Zend Framework 2

After taking a short break from Zend Framework 2 I started working on a new project in it. I created a Form class that looks like:

The problem arose when I validated the form and tried to access the data in the controller:

However, for some reason, the output of $data was missing values, something of this sort:

So, the getData() keys were present, but values were missing!

It took me a while to figure out what’s going on. In order to validate the form, Zend Framework 2 calls getInputFilter on it. However, it does not store the resulting InputFilter, but rather calls this method again whenever it needs it – essentially overwriting any data put into it by $form->setData($postData);

The solution was to store form’s InputFilter as a static variable. The code now looks like this and behaves as expected:

Output of getData() looks like:

Looks like a bug at the first glance, but I suppose this behavior allows for bigger flexibility when validating forms. I still have to find the good use for it, but oh well…