Skip to content

Commit 82006fd

Browse files
committed
bug #16886 [Form] [ChoiceType] Prefer placeholder to empty_value (boite)
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
2 parents e457a01 + 686d265 commit 82006fd

File tree

2 files changed

+134
-4
lines changed

2 files changed

+134
-4
lines changed

Extension/Core/Type/ChoiceType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ public function configureOptions(OptionsResolver $resolver)
345345
if (!is_object($options['empty_value']) || !$options['empty_value'] instanceof \Exception) {
346346
@trigger_error(sprintf('The form option "empty_value" of the "%s" form type (%s) is deprecated since version 2.6 and will be removed in 3.0. Use "placeholder" instead.', $that->getName(), __CLASS__), E_USER_DEPRECATED);
347347

348-
$placeholder = $options['empty_value'];
348+
if (null === $placeholder || '' === $placeholder) {
349+
$placeholder = $options['empty_value'];
350+
}
349351
}
350352

351353
if ($options['multiple']) {

Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 131 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ public function testPassPlaceholderToView($multiple, $expanded, $required, $plac
18051805
));
18061806
$view = $form->createView();
18071807

1808-
$this->assertEquals($viewValue, $view->vars['placeholder']);
1808+
$this->assertSame($viewValue, $view->vars['placeholder']);
18091809
$this->assertFalse($view->vars['placeholder_in_choices']);
18101810
}
18111811

@@ -1825,9 +1825,9 @@ public function testPassEmptyValueBC($multiple, $expanded, $required, $placehold
18251825
));
18261826
$view = $form->createView();
18271827

1828-
$this->assertEquals($viewValue, $view->vars['placeholder']);
1828+
$this->assertSame($viewValue, $view->vars['placeholder']);
18291829
$this->assertFalse($view->vars['placeholder_in_choices']);
1830-
$this->assertEquals($viewValue, $view->vars['empty_value']);
1830+
$this->assertSame($viewValue, $view->vars['empty_value']);
18311831
$this->assertFalse($view->vars['empty_value_in_choices']);
18321832
}
18331833

@@ -1894,6 +1894,134 @@ public function getOptionsWithPlaceholder()
18941894
);
18951895
}
18961896

1897+
/**
1898+
* @dataProvider getOptionsWithPlaceholderAndEmptyValue
1899+
* @group legacy
1900+
*/
1901+
public function testPlaceholderOptionWithEmptyValueOption($multiple, $expanded, $required, $placeholder, $emptyValue, $viewValue)
1902+
{
1903+
$form = $this->factory->create('choice', null, array(
1904+
'multiple' => $multiple,
1905+
'expanded' => $expanded,
1906+
'required' => $required,
1907+
'placeholder' => $placeholder,
1908+
'empty_value' => $emptyValue,
1909+
'choices' => $this->choices,
1910+
));
1911+
$view = $form->createView();
1912+
1913+
$this->assertSame($viewValue, $view->vars['placeholder']);
1914+
$this->assertFalse($view->vars['placeholder_in_choices']);
1915+
}
1916+
1917+
public function getOptionsWithPlaceholderAndEmptyValue()
1918+
{
1919+
return array(
1920+
// single non-expanded, not required
1921+
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, false, null),
1922+
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, null, null),
1923+
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, '', null),
1924+
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, 'bar', null),
1925+
'A placeholder is not used if empty_value is set to false [maintains BC]' => array(false, false, false, null, false, null),
1926+
'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, ''),
1927+
'An empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, false, false, null, '', ''),
1928+
'A non-empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, false, false, null, 'bar', 'bar'),
1929+
'A placeholder is not used if it is an empty string and empty_value is set to false [maintains BC]' => array(false, false, false, '', false, null),
1930+
'An unset empty_value is automatically made an empty string in a non-required field (but null is expected here) [maintains BC]' => array(false, false, false, '', null, null),
1931+
'An empty string empty_value is used if placeholder is also an empty string [maintains BC]' => array(false, false, false, '', '', ''),
1932+
'A non-empty string empty_value is used if placeholder is an empty string [maintains BC]' => array(false, false, false, '', 'bar', 'bar'),
1933+
'A non-empty string placeholder takes precedence over an empty_value set to false' => array(false, false, false, 'foo', false, 'foo'),
1934+
'A non-empty string placeholder takes precendece over a not set empty_value' => array(false, false, false, 'foo', null, 'foo'),
1935+
'A non-empty string placeholder takes precedence over an empty string empty_value' => array(false, false, false, 'foo', '', 'foo'),
1936+
'A non-empty string placeholder takes precedence over a non-empty string empty_value' => array(false, false, false, 'foo', 'bar', 'foo'),
1937+
// single non-expanded, required
1938+
'A placeholder is not used if it is explicitly set to false' => array(false, false, true, false, false, null),
1939+
'A placeholder is not used if it is explicitly set to false' => array(false, false, true, false, null, null),
1940+
'A placeholder is not used if it is explicitly set to false' => array(false, false, true, false, '', null),
1941+
'A placeholder is not used if it is explicitly set to false' => array(false, false, true, false, 'bar', null),
1942+
'A placeholder is not used if empty_value is set to false [maintains BC]' => array(false, false, true, null, false, null),
1943+
'A placeholder is not used if empty_value is not set [maintains BC]' => array(false, false, true, null, null, null),
1944+
'An empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, false, true, null, '', ''),
1945+
'A non-empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, false, true, null, 'bar', 'bar'),
1946+
'A placeholder is not used if it is an empty string and empty_value is set to false [maintains BC]' => array(false, false, true, '', false, null),
1947+
'A placeholder is not used if empty_value is not set [maintains BC]' => array(false, false, true, '', null, null),
1948+
'An empty string empty_value is used if placeholder is also an empty string [maintains BC]' => array(false, false, true, '', '', ''),
1949+
'A non-empty string empty_value is used if placeholder is an empty string [maintains BC]' => array(false, false, true, '', 'bar', 'bar'),
1950+
'A non-empty string placeholder takes precedence over an empty_value set to false' => array(false, false, true, 'foo', false, 'foo'),
1951+
'A non-empty string placeholder takes precendece over a not set empty_value' => array(false, false, true, 'foo', null, 'foo'),
1952+
'A non-empty string placeholder takes precedence over an empty string empty_value' => array(false, false, true, 'foo', '', 'foo'),
1953+
'A non-empty string placeholder takes precedence over a non-empty string empty_value' => array(false, false, true, 'foo', 'bar', 'foo'),
1954+
// single expanded, not required
1955+
'A placeholder is not used if it is explicitly set to false' => array(false, true, false, false, false, null),
1956+
'A placeholder is not used if it is explicitly set to false' => array(false, true, false, false, null, null),
1957+
'A placeholder is not used if it is explicitly set to false' => array(false, true, false, false, '', null),
1958+
'A placeholder is not used if it is explicitly set to false' => array(false, true, false, false, 'bar', null),
1959+
'A placeholder is not used if empty_value is set to false [maintains BC]' => array(false, true, false, null, false, null),
1960+
'An unset empty_value is automaticaly made an empty string in a non-required field (but null is expected here) [maintains BC]' => array(false, true, false, null, null, null),
1961+
'An empty string empty_value is converted to "None" in an expanded single choice field [maintains BC]' => array(false, true, false, null, '', 'None'),
1962+
'A non-empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, true, false, null, 'bar', 'bar'),
1963+
'A placeholder is not used if it is an empty string and empty_value is set to false [maintains BC]' => array(false, true, false, '', false, null),
1964+
'An unset empty_value is automatically made an empty string in a non-required field (but null is expected here) [maintains BC]' => array(false, true, false, '', null, null),
1965+
'An empty string empty_value is converted to "None" in an expanded single choice field [maintains BC]' => array(false, true, false, '', '', 'None'),
1966+
'A non-empty string empty_value is used if placeholder is an empty string [maintains BC]' => array(false, true, false, '', 'bar', 'bar'),
1967+
'A non-empty string placeholder takes precedence over an empty_value set to false' => array(false, true, false, 'foo', false, 'foo'),
1968+
'A non-empty string placeholder takes precendece over a not set empty_value' => array(false, true, false, 'foo', null, 'foo'),
1969+
'A non-empty string placeholder takes precedence over an empty string empty_value' => array(false, true, false, 'foo', '', 'foo'),
1970+
'A non-empty string placeholder takes precedence over a non-empty string empty_value' => array(false, true, false, 'foo', 'bar', 'foo'),
1971+
// single expanded, required
1972+
'A placeholder is not used if it is explicitly set to false' => array(false, true, true, false, false, null),
1973+
'A placeholder is not used if it is explicitly set to false' => array(false, true, true, false, null, null),
1974+
'A placeholder is not used if it is explicitly set to false' => array(false, true, true, false, '', null),
1975+
'A placeholder is not used if it is explicitly set to false' => array(false, true, true, false, 'bar', null),
1976+
'A placeholder is not used if empty_value is set to false [maintains BC]' => array(false, true, true, null, false, null),
1977+
'A placeholder is not used if empty_value is not set [maintains BC]' => array(false, true, true, null, null, null),
1978+
'An empty string empty_value is converted to "None" in an expanded single choice field [maintains BC]' => array(false, true, true, null, '', 'None'),
1979+
'A non-empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, true, true, null, 'bar', 'bar'),
1980+
'A placeholder is not used if it is an empty string and empty_value is set to false [maintains BC]' => array(false, true, true, '', false, null),
1981+
'A placeholder is not used if empty_value is not set [maintains BC]' => array(false, true, true, '', null, null),
1982+
'An empty string empty_value is converted to "None" in an expanded single choice field [maintains BC]' => array(false, true, true, '', '', 'None'),
1983+
'A non-empty string empty_value is used if placeholder is an empty string [maintains BC]' => array(false, true, true, '', 'bar', 'bar'),
1984+
'A non-empty string placeholder takes precedence over an empty_value set to false' => array(false, true, true, 'foo', false, 'foo'),
1985+
'A non-empty string placeholder takes precendece over a not set empty_value' => array(false, true, true, 'foo', null, 'foo'),
1986+
'A non-empty string placeholder takes precedence over an empty string empty_value' => array(false, true, true, 'foo', '', 'foo'),
1987+
'A non-empty string placeholder takes precedence over a non-empty string empty_value' => array(false, true, true, 'foo', 'bar', 'foo'),
1988+
// multiple expanded, not required
1989+
array(true, true, false, false, false, null),
1990+
array(true, true, false, false, null, null),
1991+
array(true, true, false, false, '', null),
1992+
array(true, true, false, false, 'bar', null),
1993+
array(true, true, false, null, false, null),
1994+
array(true, true, false, null, null, null),
1995+
array(true, true, false, null, '', null),
1996+
array(true, true, false, null, 'bar', null),
1997+
array(true, true, false, '', false, null),
1998+
array(true, true, false, '', null, null),
1999+
array(true, true, false, '', '', null),
2000+
array(true, true, false, '', 'bar', null),
2001+
array(true, true, false, 'foo', false, null),
2002+
array(true, true, false, 'foo', null, null),
2003+
array(true, true, false, 'foo', '', null),
2004+
array(true, true, false, 'foo', 'bar', null),
2005+
// multiple expanded, required
2006+
array(true, true, true, false, false, null),
2007+
array(true, true, true, false, null, null),
2008+
array(true, true, true, false, '', null),
2009+
array(true, true, true, false, 'bar', null),
2010+
array(true, true, true, null, false, null),
2011+
array(true, true, true, null, null, null),
2012+
array(true, true, true, null, '', null),
2013+
array(true, true, true, null, 'bar', null),
2014+
array(true, true, true, '', false, null),
2015+
array(true, true, true, '', null, null),
2016+
array(true, true, true, '', '', null),
2017+
array(true, true, true, '', 'bar', null),
2018+
array(true, true, true, 'foo', false, null),
2019+
array(true, true, true, 'foo', null, null),
2020+
array(true, true, true, 'foo', '', null),
2021+
array(true, true, true, 'foo', 'bar', null),
2022+
);
2023+
}
2024+
18972025
public function testPassChoicesToView()
18982026
{
18992027
$choices = array('A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd');

0 commit comments

Comments
 (0)