Skip to content

[Form] Fixed show float values as choice value in ChoiceType #20378

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

Merged
merged 1 commit into from
Nov 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ private function castableToString(array $choices, array &$cache = array())
continue;
} elseif (!is_scalar($choice)) {
return false;
} elseif (isset($cache[$choice])) {
}

$choice = false === $choice ? '0' : (string) $choice;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we simplify this a bit and just always use var_export()?

Copy link
Member Author

@yceruto yceruto Nov 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your idea (perhaps that allows render "false" instead of "0" for boolean values) but we need to detect duplicates now when (0 and false) or (1 and true) coexist in the same choices, else it breaks the current behavior when above these values are casted to (string) again.

Copy link
Member Author

@yceruto yceruto Nov 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, the result looks weird by using var_export() for float values in some PHP versions, for instance https://3v4l.org/beeGa#v5422 😕 ?


if (isset($cache[$choice])) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ protected function createChoiceList()

protected function getChoices()
{
return array(0, 1, '1', 'a', false, true, $this->object, null);
return array(0, 1, 1.5, '1', 'a', false, true, $this->object, null);
}

protected function getValues()
{
return array('0', '1', '2', '3', '4', '5', '6', '7');
return array('0', '1', '2', '3', '4', '5', '6', '7', '8');
}

/**
Expand Down Expand Up @@ -162,4 +162,13 @@ public function testGetChoicesForValuesWithContainingEmptyStringAndBooleans()
$this->assertSame(array(0 => true), $choiceList->getChoicesForValues(array('1')));
$this->assertSame(array(0 => false), $choiceList->getChoicesForValues(array('0')));
}

public function testGetChoicesForValuesWithContainingEmptyStringAndFloats()
{
$choiceList = new ArrayChoiceList(array('Empty String' => '', '1/3' => 0.3, '1/2' => 0.5));

$this->assertSame(array(0 => ''), $choiceList->getChoicesForValues(array('')));
$this->assertSame(array(0 => 0.3), $choiceList->getChoicesForValues(array('0.3')));
$this->assertSame(array(0 => 0.5), $choiceList->getChoicesForValues(array('0.5')));
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy