Issues

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

This issue has been fixed in RC4 https://github.com/zendframework/zf2/…