I have a ZF2 form where I had to disable native validators, for a specific reason.
Then, when adding elements programatically to the form I also add validators.
One of the elements is a Multiselect array.
$form->add( array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
(
'label' => 'few items',
'value_options' => Array
(
'one' => 'one',
'two' => 'two',
'three' => 'three',
'four' => 'four',
)
),
'attributes' => array
(
'multiple' => 'multiple',
'value' => array('two','three'),
'required' => 1,
'id' => 'few_items'
),
'name' => 'few_items'
));
Also, I'm going to add an InArray validator:
if($f instanceof \Zend\Form\Element\Select){
$inputFilter->add($factory->createInput(array(
'name' => $f->getName(),
'required' => $f->getAttribute('required') == 1,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => $f->getValueOptions(),
'messages' => array(
InArray::NOT_IN_ARRAY => 'Please select an option',
),
),
),
),
)));
}
The problem is that the validator always fails, because in POST multiselect field will return an array, and actually looking inside the InArray validator, it uses in_array(...) PHP function which is not suitable for this - array_intersect would do the trick, but before writing my own validator I do have a feeling that this wheel was invented already!
Having looked around I see that there was a bug raised to this effect (http://framework.zend.com/issues/browse/ZF2-413), and the solution was to introduce Explode validator, but I'm not sure how to add it into my input filter.
Thanks for your suggestions.