Issues

ZF2-466: Zend\Form\Element\Select Default Validator causes false-negative results

Issue Type: Bug Created: 2012-08-15T13:06:57.000+0000 Last Updated: 2012-08-17T07:32:03.000+0000 Status: Resolved Fix version(s): Reporter: Daniel Müller (damu) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend\Form

  • Validator
  • Zend\Form\Element\Select

Related issues: Attachments:

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

<pre class="highlight">
// 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

<pre class="highlight">
protected function getValidator()
    {
        if (null === $this->validator) {
            $this->validator = new InArrayValidator(array(
                'haystack' => (array) $this->getAttribute('options'),
                'strict'   => false
            ));
        }
        return $this->validator;
    }

By

<pre class="highlight">
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/…

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts