ZF2-466: Zend\Form\Element\Select Default Validator causes false-negative results
Description
Issue description
Select::getValidator() adds an InArrayValidator()
However, select submit values are array keys, not array values.
Thus, when having an id-name list, the validator will fail in most or all cases.
Code to reproduce
// In Controller
$pt = new Zend\Form\Element\Select('name', array(
'label' => 'Select one',
));
$pt->setAttributes(array(
'options' => array(1 => 'Option 1', 2 => 'Option 2'),
));
$form->add($pt);
// In View
echo $this->formRow($pt);
Possible fix
Replace
protected function getValidator()
{
if (null === $this->validator) {
$this->validator = new InArrayValidator(array(
'haystack' => (array) $this->getAttribute('options'),
'strict' => false
));
}
return $this->validator;
}
By
protected function getValidator()
{
if (null === $this->validator) {
$this->validator = new InArrayValidator(array(
'haystack' => array_keys((array) $this->getAttribute('options')),
'strict' => false
));
}
return $this->validator;
}
Tested with Current RC3, pulled on 15th of August, 3pm (CET)
The validator was added in this commit: https://github.com/zendframework/zf2/…
Comments
Posted by Daniel Müller (damu) on 2012-08-17T07:32:03.000+0000
This issue has been fixed in RC4 https://github.com/zendframework/zf2/…