Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
michalbundyra opened this issue Jan 15, 2020 · 5 comments · Fixed by #116
Closed

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

michalbundyra opened this issue Jan 15, 2020 · 5 comments · Fixed by #116
Labels
Bug Something isn't working
Milestone

Comments

@michalbundyra
Copy link
Member

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);

Originally posted by @googlle at zendframework/zend-form#18

@michalbundyra michalbundyra added the Bug Something isn't working label Jan 15, 2020
@michalbundyra
Copy link
Member Author

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);
}

Originally posted by @adamlundrigan at zendframework/zend-form#18 (comment)

@michalbundyra
Copy link
Member Author

@michalbundyra
Copy link
Member Author

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.


Originally posted by @adamlundrigan at zendframework/zend-form#18 (comment)

@driehle
Copy link
Contributor

driehle commented May 27, 2021

somehow the linked pull request hasn't closed this issue...

@Slamdunk
Copy link
Contributor

Closed by #116

somehow the linked pull request hasn't closed this issue...

That's because nowhere in the PR nor in the merge commit there's a Close word

@Slamdunk Slamdunk added this to the 3.0.0 milestone May 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants