Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Form] ChoiceType: Fix cast to string from null choice#21160

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
yceruto wants to merge1 commit intosymfony:2.7fromyceruto:fix_choices

Conversation

@yceruto
Copy link
Member

@ycerutoyceruto commentedJan 4, 2017
edited
Loading

QA
Branch?2.7
Bug fix?yes
New feature?no
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets-
LicenseMIT
Doc PRsymfony/symfony-docs#6393

Given the following:

$builder->add('attending','choice', ['choices' => ['Yes' =>true,'No' =>false,'Maybe' =>null,        ]    ]);;

Before:

<selectid="form_attending"name="form[attending]"><optionvalue="0">Yes</option><optionvalue="1">No</option><optionvalue="2"selected="selected">Maybe</option></select>

After:

<selectid="form_attending"name="form[attending]"><optionvalue="1">Yes</option><optionvalue="0">No</option><optionvalue=""selected="selected">Maybe</option></select>

This results more consistent with expected behavior.

@HeahDude
Copy link
Contributor

Hey@yceruto thanks for opening this PR but as discussed in#17760 with@Tobion it would then collide with the placeholder value which is always an empty string.

It also means we're missing a test for this regression.

yceruto reacted with confused emoji

@yceruto
Copy link
MemberAuthor

yceruto commentedJan 4, 2017
edited
Loading

it would then collide with the placeholder value which is always an empty string.

If I understood correctly (#17760 (comment)) then this PR don't affects the current behavior, because currently this already happen:

#17760 (comment) ... having both a placeholder and a choice with an explicit null or empty string '' value, there is a conflict... The default null choice is preselected over the placeholder added in the choice list.

@HeahDude
Copy link
Contributor

when having both a placeholder and a choice with an explicit null or empty string '' value, there is a conflict... The default null choice is preselected over the placeholder added in the choice list.

Well it shouldn'thttps://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php#L207.

@yceruto
Copy link
MemberAuthor

yceruto commentedJan 4, 2017
edited
Loading

Well it shouldn't

Agreed, but IMHO this PR is not the source of bad behavior, this already happen before that:

Before this PR:

Result (should show two options and placeholder selected):

$builder->add('foo','choice',array('choices' =>array('None' =>null),'placeholder' =>'Please choose',))
<selectid="form_foo"name="form[foo]"required="required"><optionvalue="">Please choose</option><optionvalue="0"selected="selected">None</option> // wrong selected</select>

Result (should show three options and placeholder selected):

$builder->add('foo','choice',array('choices' =>array('Red' =>'red','None' =>''),'placeholder' =>'Please choose',))
<selectid="form_foo"name="form[foo]"required="required"><optionvalue=""selected="selected">Please choose</option><optionvalue="red">Red</option><optionvalue=""selected="selected">None</option> //  wrong selected</select>

After this PR:

Result (should show two options and placeholder selected):

$builder->add('foo','choice',array('choices' =>array('None' =>null),// or array('None' => ''),'placeholder' =>'Please choose',))
<selectid="form_foo"name="form[foo]"required="required"><optionvalueselected="selected">None</option></select>

Result (should show three options and placeholder selected):

$builder->add('foo','choice',array('choices' =>array('Red' =>'red','None' =>null),// or 'None' => '''placeholder' =>'Please choose',))
<selectid="form_foo"name="form[foo]"required="required"><optionvalue=""selected="selected">Please choose</option><optionvalue="red">Red</option><optionvalue=""selected="selected">None</option> //  wrong selected</select>

The result in all cases (before/after) is wrong in any way.

This PR resolves an inconsistency issue, but do you think that makes the problem worse?

@yceruto
Copy link
MemberAuthor

yceruto commentedJan 4, 2017
edited
Loading

Maybe this result after:

<selectid="form_foo"name="form[foo]"required="required">     // placeholder option ignored because there is a choice with "" or null.<optionvalueselected="selected">None</option></select>

should be the correct behavior andplaceholder should be ignored when there is a choice with'' ornull, because in both case the result on submit should be the same:null.

EDIT: Just asplaceholder is ignored whenmultiple istrue.

@yceruto
Copy link
MemberAuthor

yceruto commentedJan 4, 2017
edited
Loading

Otherwise, we need achieve render two options with the same empty value:

<selectid="form_foo"name="form[foo]"><optionvalueselected="selected">Please choose</option><optionvalue>None</option></select>

Is there the expected behavior?

@HeahDude
Copy link
Contributor

IMHO both results before your PR are expected:


$builder->add('foo','choice',array('choices' =>array('None' =>null),'placeholder' =>'Please choose',))
<selectid="form_foo"name="form[foo]"required="required"><optionvalue="">Please choose</option><optionvalue="0"selected="selected">None</option> // wrong selected</select>

I don't think this is wrongly selected if the data passed on form creation isnull, then the choice with a model datanull and value0 is selected.


$builder->add('foo','choice',array('choices' =>array('None' =>''),'placeholder' =>'Please choose',))
<selectid="form_foo"name="form[foo]"required="required"><optionvalueselected="selected">None</option> // missing placeholder and wrong selected</select>

The pre selection here is tricky but expected and the missing placeholder is expected toohttps://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/ChoiceList/View/ChoiceListView.php#L55.


The standard is about having the placeholder first because for a select input, the first value is always selected. By default, the empty string in this context means "I haven't choose anything yet", and it makes sense in some cases to bind it to an empty string or null in the model.

The thing is that it does not make sense to use an empty string as model choice (which may not be the first) and use the placeholder option at the same time, because even in this edge case thechoice_value option can solve it..

If we want more control on it, we should consider it a feature and maybe add aplaceholder_value option to define it, but I don't think it's worth it, let's improve the docs instead.

@yceruto
Copy link
MemberAuthor

👍 Fair enough then :)

@ycerutoyceruto closed thisJan 4, 2017
@ycerutoyceruto deleted the fix_choices branchJanuary 4, 2017 22:55
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@yceruto@HeahDude@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp