0

I have an issue when adding a filter to my select elements in Zend 2.2. When creating a select element, a default validator is added to this element:

This element adds an InArray validator to its input filter specification in order to validate on the server if the selected value belongs to the values.

This works fine until I add a custom filter to that element:

$filter = new Input('element_name');
$filter->setRequired(true);
$this->add($filter);

This Zend\InputFilter\Input is added to a Zend\InputFilter\InputFilter which is later added to my form. From now on the default validator is lost.

How can I add a filter to form elements without losing their default validators?

Am I missing something? I don't expect this as the default behaviour, because everyone is desperately asking how to deactivate it. There's even an attribute to deactivate it ('disable_inarray_validator'), however, I want to keep that validator without passing all my entries of the selectbox to my InputFilter. Also I didn't find anything in the Zend 2 documentation. I guess the solution must be something simple. Thanks in advance.

Note: Same happens when I add the InputFilter with the InputFilterManager to the form.

nick
  • 92
  • 1
  • 8
  • Would love to know the answer to this too. I spent quite a few hours trying to get this to work. I tried using the form manager to get the form in my controller rather than instantiating the form directly. Also tried adding my fields in init() method rather than the __construct. Tried creating input filters in mutiple ways, used $factory->createInput in my input filters. Also tried using the $form->setPreferFormInputFilter() method with no success. Seems like a bug or I am missing something. Please post answer if you find one – Purple Hexagon Jan 16 '14 at 21:21

1 Answers1

0

Please see my comment on your question first. Only thing I thought might work that I haven't tried to get this to work is:

$input->getFilterChain()
      ->attachByName('stringtrim')

or

$input->getFilterChain()
    ->attach(new StringTrim())
Purple Hexagon
  • 3,538
  • 2
  • 24
  • 45
  • Hi, thanks for your reply. Glad to know I'm not alone with this problem. I tried your suggestion, but it didn't help. Also, isn't the ValidatorChain more important? How would the FilterChain affect the default validator? – nick Jan 17 '14 at 08:06
  • Sorry was a bit late when I wrote that and had been trying to solve this issue on and off all day. My thinking was though if you get the filter chain and validation chain separately then you shouldn't overwrite the default validator. Also I am only dealing with default filters so may have confused my issue and yours. I am going to ticket this up on github or on the zf2 mailing list as it seems like a bug. Some stuff in here seems relevant https://github.com/zendframework/zf2/pull/3218 – Purple Hexagon Jan 17 '14 at 08:15
  • Thanks for the link, it made a few points clear. It seems like a bug to me too, in the Zend\Form\Form class there is an attribute $useInputFilterDefaults which defaults to true. But this is obviously not working. Can you post a link to your ticket/mailing list? – nick Jan 17 '14 at 08:47
  • Will do once I create yeah. Going to write a few unit tests etc for that can be run to highlight the issue. Should get time in the next few hours though – Purple Hexagon Jan 17 '14 at 10:35