Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

When calling setValue Element\Select, selected incorrect values #18

Open
googlle opened this issue Oct 3, 2015 · 4 comments
Open

When calling setValue Element\Select, selected incorrect values #18

googlle opened this issue Oct 3, 2015 · 4 comments
Labels

Comments

@googlle
Copy link

googlle commented Oct 3, 2015

When calling $select->setValue(array('1.1')), selected values French and Japanese,
but Japanese is incorrect value

use Zend\Form\Element;
use Zend\Form\Form;

$select = new Element\Select('language');
$select->setLabel('Which is your mother tongue?');
$select->setValueOptions(array(
    '1.1' => 'French',
    '1.2' => 'English',
    '1.10' => 'Japanese',
    '1.20' => 'Chinese',
));

$form = new Form('language');
$form->add($select);
@adamlundrigan
Copy link
Contributor

Confirmed. The following test fails when added to ZendTest\Form\View\Helper\FormSelectTest:

public function testIssue18()
{
    $select = new SelectElement('language');
    $select->setLabel('Which is your mother tongue?');
    $select->setAttribute('multiple', true);
    $select->setValueOptions(array(
        '1.1' => 'French',
        '1.2' => 'English',
        '1.10' => 'Japanese',
        '1.20' => 'Chinese',
    ));
    $select->setValue(array('1.1'));        
    $this->assertEquals(array('1.1'), $select->getValue());

    $markup  = $this->helper->render($select);

    $this->assertRegExp('{value="1.1" selected="selected"}i', $markup);
    $this->assertNotRegExp('{value="1.2" selected="selected"}i', $markup);
    $this->assertNotRegExp('{value="1.10" selected="selected"}i', $markup);
    $this->assertNotRegExp('{value="1.20" selected="selected"}i', $markup);
}

@adamlundrigan
Copy link
Contributor

@adamlundrigan
Copy link
Contributor

The underlying issue here is with Zend\Stdlib\ArrayUtils#inArray. The protection added there to protect against wonky non-strict checks (by casting everything to string) bumps up against a different oddity in PHP: truncating trailing zeroes, making "1.1" and "1.10" equivalent:

<?php
$needle = '1.10';
$haystack = ['1.1'];

assert(in_array($needle, $haystack) === false);
// PHP Warning:  assert(): Assertion failed in <file> on line 5

(3v4l: https://3v4l.org/HKM8Q)

The last argument of the in_array call in Zend\Stdlib\ArrayUtils#inArray should be changed to always be true.

@michalbundyra
Copy link
Member

This repository has been closed and moved to laminas/laminas-form; a new issue has been opened at laminas/laminas-form#52.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants