Skip to content

[Form] [ChoiceType] Prefer placeholder to empty_value #16886

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

Closed
wants to merge 4 commits into from

Conversation

boite
Copy link
Contributor

@boite boite commented Dec 7, 2015

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #16885
License MIT
Doc PR -

Prefer an explicitly set placeholder option (i.e. false or a non-empty
string) to an empty_value option when both are set.

The fix is to change the behaviour in the placeholder normalizer in
ChoiceType::configureOptions so that the value of the empty_value option is
used for placeholder only when the value of placeholder is null or an empty
string.

@boite boite force-pushed the ticket_16885 branch 4 times, most recently from 4271ab1 to 4dec2c8 Compare December 14, 2015 10:24
Use assertSame instead of assertEquals in some ChoiceTypeTest methods
which test placeholder/empty_value options, because the tested value
may be any number of "falsey" values.
Add an extra condition to the use of the value of the empty_value option
as the placeholder: the value of the placeholder option must be null or
an empty string (i.e. not explicitly set).

Test the effects of setting both placeholder and empty_value.
'An unset empty_value is automaticaly made an empty string in a non-required field (but null is expected here) [maintains BC]' => array(false, false, false, null, null, ''),
'An empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, false, false, null, '', ''),
'A non-empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, false, false, null, 'bar', 'bar'),
'A placeholder is not used if it is an empty string and empty_value is set to false [mainatins BC]' => array(false, false, false, '', false, null),
Copy link
Contributor

Choose a reason for hiding this comment

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

typo "maintains"

@HeahDude
Copy link
Contributor

👍 except your last test being very difficult to read.

Perhaps you could organise lines by BC first, then by expanded or multiple.
Rephrasing might help like :
"If placeholder is null", "If placeholder is false and empty_value is empty" ...

@xabbuh
Copy link
Member

xabbuh commented Feb 18, 2016

The fix looks good, but do we really need so many test fixtures? Can we reduce them to the bare minimum?

@boite
Copy link
Contributor Author

boite commented Feb 28, 2016

@xabbuh the (admittedly extensive) test data were invaluable in that they revealed when the fix was finally correct after a number of failed attempts. Surely, one datum for each possible permutation is the bare minimum.

$placeholder,
$emptyValue,
$viewValue
) {
Copy link
Member

Choose a reason for hiding this comment

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

should be moved on one line

@fabpot
Copy link
Member

fabpot commented Feb 29, 2016

👍

@webmozart
Copy link
Contributor

👍

@fabpot
Copy link
Member

fabpot commented Mar 2, 2016

Thank you @boite.

fabpot added a commit that referenced this pull request Mar 2, 2016
This PR was squashed before being merged into the 2.7 branch (closes #16886).

Discussion
----------

[Form] [ChoiceType] Prefer placeholder to empty_value

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #16885
| License       | MIT
| Doc PR        | -

Prefer an explicitly set `placeholder` option (i.e. `false` or a non-empty
string) to an `empty_value` option when both are set.

The fix is to change the behaviour in the placeholder normalizer in
ChoiceType::configureOptions so that the value of the `empty_value` option is
used for placeholder only when the value of `placeholder` is null or an empty
string.

Commits
-------

a4d4c8a [Form] [ChoiceType] Prefer placeholder to empty_value
@fabpot fabpot closed this Mar 2, 2016
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, null, null),
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, '', null),
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, 'bar', null),
'A placeholder is not used if empty_value is set to false [maintains BC]' => array(false, false, false, null, false, null),
Copy link
Contributor

Choose a reason for hiding this comment

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

All those tests may not run because some keys are overridden...

Copy link
Member

Choose a reason for hiding this comment

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

indeed

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm on it

@HeahDude
Copy link
Contributor

HeahDude commented Mar 2, 2016

Here's the actual report :

There were 8 failures:

1) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testPlaceholderOptionWithEmptyValueOption with data set "An empty string empty_value is converted to "None" in an expanded single choice field when expanded and required [maintains BC]" (false, true, true, null, '', 'None')
Failed asserting that null is identical to 'None'.

/Users/Heah/Sites/dev/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php:1913

2) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testPlaceholderOptionWithEmptyValueOption with data set "A non-empty string empty_value is used if placeholder is not set when expanded and required [maintains BC]" (false, true, true, null, 'bar', 'bar')
Failed asserting that null is identical to 'bar'.

/Users/Heah/Sites/dev/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php:1913

3) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testPlaceholderOptionWithEmptyValueOption with data set "An empty string empty_value is converted to "None" in an expanded single choice field when required [maintains BC]" (false, true, true, '', '', 'None')
Failed asserting that null is identical to 'None'.

/Users/Heah/Sites/dev/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php:1913

4) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testPlaceholderOptionWithEmptyValueOption with data set "A non-empty string empty_value is used if placeholder is an empty string when expanded and required [maintains BC]" (false, true, true, '', 'bar', 'bar')
Failed asserting that null is identical to 'bar'.

/Users/Heah/Sites/dev/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php:1913

5) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testPlaceholderOptionWithEmptyValueOption with data set "A non-empty string placeholder takes precedence over an empty_value set to false when expanded and required" (false, true, true, 'foo', false, 'foo')
Failed asserting that null is identical to 'foo'.

/Users/Heah/Sites/dev/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php:1913

6) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testPlaceholderOptionWithEmptyValueOption with data set "A non-empty string placeholder takes precedence over a not set empty_value when expanded and required" (false, true, true, 'foo', null, 'foo')
Failed asserting that null is identical to 'foo'.

/Users/Heah/Sites/dev/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php:1913

7) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testPlaceholderOptionWithEmptyValueOption with data set "A non-empty string placeholder takes precedence over an empty string empty_value when expanded and required" (false, true, true, 'foo', '', 'foo')
Failed asserting that null is identical to 'foo'.

/Users/Heah/Sites/dev/symfony/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php:1913

8) Symfony\Component\Form\Tests\Extension\Core\Type\ChoiceTypeTest::testPlaceholderOptionWithEmptyValueOption with data set "A non-empty string placeholder takes precedence over a non-empty string empty_value when expanded and required" (false, true, true, 'foo', 'bar', 'foo')
Failed asserting that null is identical to 'foo'.

@boite
Copy link
Contributor Author

boite commented Mar 2, 2016

Damn it! Will you let me sort this out? I'll do a better job of naming the data keys for readability (and uniqueness).

@HeahDude
Copy link
Contributor

HeahDude commented Mar 2, 2016

I fixed it, but I still can hold the PR. What do I do ?

HeahDude added a commit to HeahDude/symfony that referenced this pull request Mar 2, 2016
@HeahDude
Copy link
Contributor

HeahDude commented Mar 2, 2016

@boite, see #17988, feel free to send a PR that better suits you and I'll close this one.

fabpot added a commit that referenced this pull request Mar 3, 2016
This PR was merged into the 2.7 branch.

Discussion
----------

[Form] fix tests added by #16886

| Q             | A
| ------------- | ---
| Branch        | 2.7+
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #16886
| License       | MIT
| Doc PR        | -

Commits
-------

bd22c86 minor [Form] fix tests added by #16886
fabpot added a commit that referenced this pull request Mar 4, 2016
* 2.7:
  Updated all the README files
  [TwigBundle] Fix failing test on appveyor
  Improved the error message when using "@" in a decorated service
  Improve error reporting in router panel of web profiler
  [DoctrineBridge][Form] Fix performance regression in EntityType
  [FrameworkBundle] Fix a regression in handling absolute and namespaced template paths
  Allow to normalize \Traversable
  minor [Form] fix tests added by #16886
  Remove _path from query parameters when fragment is a subrequest and request attributes are already set Added tests for _path removal in FragmentListener
  Simplified everything
  Added a test
  Fixed the problem in an easier way
  Fixed a syntax issue
  Improved the error message when a template is not found
  [CodingStandards] Conformed to coding standards
  [TwigBundle] fixed Include file locations in "Template could not be found" exception
fabpot added a commit that referenced this pull request Mar 4, 2016
* 2.8:
  Updated all the README files
  [TwigBundle] Fix failing test on appveyor
  Improved the error message when using "@" in a decorated service
  Improve error reporting in router panel of web profiler
  [DoctrineBridge][Form] Fix performance regression in EntityType
  [FrameworkBundle] Fix a regression in handling absolute and namespaced template paths
  Allow to normalize \Traversable
  minor [Form] fix tests added by #16886
  Remove _path from query parameters when fragment is a subrequest and request attributes are already set Added tests for _path removal in FragmentListener
  Simplified everything
  Added a test
  Fixed the problem in an easier way
  Fixed a syntax issue
  Improved the error message when a template is not found
  [CodingStandards] Conformed to coding standards
  [TwigBundle] fixed Include file locations in "Template could not be found" exception
fabpot added a commit that referenced this pull request Mar 4, 2016
* 3.0:
  Updated all the README files
  [TwigBundle] Fix failing test on appveyor
  Improved the error message when using "@" in a decorated service
  Improve error reporting in router panel of web profiler
  [DoctrineBridge][Form] Fix performance regression in EntityType
  [FrameworkBundle] Fix a regression in handling absolute and namespaced template paths
  Allow to normalize \Traversable
  minor [Form] fix tests added by #16886
  Remove _path from query parameters when fragment is a subrequest and request attributes are already set Added tests for _path removal in FragmentListener
  Simplified everything
  Added a test
  Fixed the problem in an easier way
  Fixed a syntax issue
  Improved the error message when a template is not found
  [CodingStandards] Conformed to coding standards
  [TwigBundle] fixed Include file locations in "Template could not be found" exception
ostrolucky pushed a commit to ostrolucky/symfony that referenced this pull request Mar 25, 2018
* 2.7:
  Updated all the README files
  [TwigBundle] Fix failing test on appveyor
  Improved the error message when using "@" in a decorated service
  Improve error reporting in router panel of web profiler
  [DoctrineBridge][Form] Fix performance regression in EntityType
  [FrameworkBundle] Fix a regression in handling absolute and namespaced template paths
  Allow to normalize \Traversable
  minor [Form] fix tests added by symfony#16886
  Remove _path from query parameters when fragment is a subrequest and request attributes are already set Added tests for _path removal in FragmentListener
  Simplified everything
  Added a test
  Fixed the problem in an easier way
  Fixed a syntax issue
  Improved the error message when a template is not found
  [CodingStandards] Conformed to coding standards
  [TwigBundle] fixed Include file locations in "Template could not be found" exception
ostrolucky pushed a commit to ostrolucky/symfony that referenced this pull request Mar 25, 2018
* 2.8:
  Updated all the README files
  [TwigBundle] Fix failing test on appveyor
  Improved the error message when using "@" in a decorated service
  Improve error reporting in router panel of web profiler
  [DoctrineBridge][Form] Fix performance regression in EntityType
  [FrameworkBundle] Fix a regression in handling absolute and namespaced template paths
  Allow to normalize \Traversable
  minor [Form] fix tests added by symfony#16886
  Remove _path from query parameters when fragment is a subrequest and request attributes are already set Added tests for _path removal in FragmentListener
  Simplified everything
  Added a test
  Fixed the problem in an easier way
  Fixed a syntax issue
  Improved the error message when a template is not found
  [CodingStandards] Conformed to coding standards
  [TwigBundle] fixed Include file locations in "Template could not be found" exception
ostrolucky pushed a commit to ostrolucky/symfony that referenced this pull request Mar 25, 2018
* 3.0:
  Updated all the README files
  [TwigBundle] Fix failing test on appveyor
  Improved the error message when using "@" in a decorated service
  Improve error reporting in router panel of web profiler
  [DoctrineBridge][Form] Fix performance regression in EntityType
  [FrameworkBundle] Fix a regression in handling absolute and namespaced template paths
  Allow to normalize \Traversable
  minor [Form] fix tests added by symfony#16886
  Remove _path from query parameters when fragment is a subrequest and request attributes are already set Added tests for _path removal in FragmentListener
  Simplified everything
  Added a test
  Fixed the problem in an easier way
  Fixed a syntax issue
  Improved the error message when a template is not found
  [CodingStandards] Conformed to coding standards
  [TwigBundle] fixed Include file locations in "Template could not be found" exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants