Skip to content

Commit a4d4c8a

Browse files
boitefabpot
authored andcommitted
[Form] [ChoiceType] Prefer placeholder to empty_value
1 parent d222bd3 commit a4d4c8a

File tree

2 files changed

+134
-4
lines changed

2 files changed

+134
-4
lines changed

src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ public function configureOptions(OptionsResolver $resolver)
325325
if (!is_object($options['empty_value']) || !$options['empty_value'] instanceof \Exception) {
326326
@trigger_error('The form option "empty_value" is deprecated since version 2.6 and will be removed in 3.0. Use "placeholder" instead.', E_USER_DEPRECATED);
327327

328-
$placeholder = $options['empty_value'];
328+
if (null === $placeholder || '' === $placeholder) {
329+
$placeholder = $options['empty_value'];
330+
}
329331
}
330332

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

src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php

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

1640-
$this->assertEquals($viewValue, $view->vars['placeholder']);
1640+
$this->assertSame($viewValue, $view->vars['placeholder']);
16411641
$this->assertFalse($view->vars['placeholder_in_choices']);
16421642
}
16431643

@@ -1657,9 +1657,9 @@ public function testPassEmptyValueBC($multiple, $expanded, $required, $placehold
16571657
));
16581658
$view = $form->createView();
16591659

1660-
$this->assertEquals($viewValue, $view->vars['placeholder']);
1660+
$this->assertSame($viewValue, $view->vars['placeholder']);
16611661
$this->assertFalse($view->vars['placeholder_in_choices']);
1662-
$this->assertEquals($viewValue, $view->vars['empty_value']);
1662+
$this->assertSame($viewValue, $view->vars['empty_value']);
16631663
$this->assertFalse($view->vars['empty_value_in_choices']);
16641664
}
16651665

@@ -1726,6 +1726,134 @@ public function getOptionsWithPlaceholder()
17261726
);
17271727
}
17281728

1729+
/**
1730+
* @dataProvider getOptionsWithPlaceholderAndEmptyValue
1731+
* @group legacy
1732+
*/
1733+
public function testPlaceholderOptionWithEmptyValueOption($multiple, $expanded, $required, $placeholder, $emptyValue, $viewValue)
1734+
{
1735+
$form = $this->factory->create('choice', null, array(
1736+
'multiple' => $multiple,
1737+
'expanded' => $expanded,
1738+
'required' => $required,
1739+
'placeholder' => $placeholder,
1740+
'empty_value' => $emptyValue,
1741+
'choices' => $this->choices,
1742+
));
1743+
$view = $form->createView();
1744+
1745+
$this->assertSame($viewValue, $view->vars['placeholder']);
1746+
$this->assertFalse($view->vars['placeholder_in_choices']);
1747+
}
1748+
1749+
public function getOptionsWithPlaceholderAndEmptyValue()
1750+
{
1751+
return array(
1752+
// single non-expanded, not required
1753+
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, false, null),
1754+
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, null, null),
1755+
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, '', null),
1756+
'A placeholder is not used if it is explicitly set to false' => array(false, false, false, false, 'bar', null),
1757+
'A placeholder is not used if empty_value is set to false [maintains BC]' => array(false, false, false, null, false, null),
1758+
'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, ''),
1759+
'An empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, false, false, null, '', ''),
1760+
'A non-empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, false, false, null, 'bar', 'bar'),
1761+
'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),
1762+
'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),
1763+
'An empty string empty_value is used if placeholder is also an empty string [maintains BC]' => array(false, false, false, '', '', ''),
1764+
'A non-empty string empty_value is used if placeholder is an empty string [maintains BC]' => array(false, false, false, '', 'bar', 'bar'),
1765+
'A non-empty string placeholder takes precedence over an empty_value set to false' => array(false, false, false, 'foo', false, 'foo'),
1766+
'A non-empty string placeholder takes precendece over a not set empty_value' => array(false, false, false, 'foo', null, 'foo'),
1767+
'A non-empty string placeholder takes precedence over an empty string empty_value' => array(false, false, false, 'foo', '', 'foo'),
1768+
'A non-empty string placeholder takes precedence over a non-empty string empty_value' => array(false, false, false, 'foo', 'bar', 'foo'),
1769+
// single non-expanded, required
1770+
'A placeholder is not used if it is explicitly set to false' => array(false, false, true, false, false, null),
1771+
'A placeholder is not used if it is explicitly set to false' => array(false, false, true, false, null, null),
1772+
'A placeholder is not used if it is explicitly set to false' => array(false, false, true, false, '', null),
1773+
'A placeholder is not used if it is explicitly set to false' => array(false, false, true, false, 'bar', null),
1774+
'A placeholder is not used if empty_value is set to false [maintains BC]' => array(false, false, true, null, false, null),
1775+
'A placeholder is not used if empty_value is not set [maintains BC]' => array(false, false, true, null, null, null),
1776+
'An empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, false, true, null, '', ''),
1777+
'A non-empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, false, true, null, 'bar', 'bar'),
1778+
'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),
1779+
'A placeholder is not used if empty_value is not set [maintains BC]' => array(false, false, true, '', null, null),
1780+
'An empty string empty_value is used if placeholder is also an empty string [maintains BC]' => array(false, false, true, '', '', ''),
1781+
'A non-empty string empty_value is used if placeholder is an empty string [maintains BC]' => array(false, false, true, '', 'bar', 'bar'),
1782+
'A non-empty string placeholder takes precedence over an empty_value set to false' => array(false, false, true, 'foo', false, 'foo'),
1783+
'A non-empty string placeholder takes precendece over a not set empty_value' => array(false, false, true, 'foo', null, 'foo'),
1784+
'A non-empty string placeholder takes precedence over an empty string empty_value' => array(false, false, true, 'foo', '', 'foo'),
1785+
'A non-empty string placeholder takes precedence over a non-empty string empty_value' => array(false, false, true, 'foo', 'bar', 'foo'),
1786+
// single expanded, not required
1787+
'A placeholder is not used if it is explicitly set to false' => array(false, true, false, false, false, null),
1788+
'A placeholder is not used if it is explicitly set to false' => array(false, true, false, false, null, null),
1789+
'A placeholder is not used if it is explicitly set to false' => array(false, true, false, false, '', null),
1790+
'A placeholder is not used if it is explicitly set to false' => array(false, true, false, false, 'bar', null),
1791+
'A placeholder is not used if empty_value is set to false [maintains BC]' => array(false, true, false, null, false, null),
1792+
'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),
1793+
'An empty string empty_value is converted to "None" in an expanded single choice field [maintains BC]' => array(false, true, false, null, '', 'None'),
1794+
'A non-empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, true, false, null, 'bar', 'bar'),
1795+
'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),
1796+
'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),
1797+
'An empty string empty_value is converted to "None" in an expanded single choice field [maintains BC]' => array(false, true, false, '', '', 'None'),
1798+
'A non-empty string empty_value is used if placeholder is an empty string [maintains BC]' => array(false, true, false, '', 'bar', 'bar'),
1799+
'A non-empty string placeholder takes precedence over an empty_value set to false' => array(false, true, false, 'foo', false, 'foo'),
1800+
'A non-empty string placeholder takes precendece over a not set empty_value' => array(false, true, false, 'foo', null, 'foo'),
1801+
'A non-empty string placeholder takes precedence over an empty string empty_value' => array(false, true, false, 'foo', '', 'foo'),
1802+
'A non-empty string placeholder takes precedence over a non-empty string empty_value' => array(false, true, false, 'foo', 'bar', 'foo'),
1803+
// single expanded, required
1804+
'A placeholder is not used if it is explicitly set to false' => array(false, true, true, false, false, null),
1805+
'A placeholder is not used if it is explicitly set to false' => array(false, true, true, false, null, null),
1806+
'A placeholder is not used if it is explicitly set to false' => array(false, true, true, false, '', null),
1807+
'A placeholder is not used if it is explicitly set to false' => array(false, true, true, false, 'bar', null),
1808+
'A placeholder is not used if empty_value is set to false [maintains BC]' => array(false, true, true, null, false, null),
1809+
'A placeholder is not used if empty_value is not set [maintains BC]' => array(false, true, true, null, null, null),
1810+
'An empty string empty_value is converted to "None" in an expanded single choice field [maintains BC]' => array(false, true, true, null, '', 'None'),
1811+
'A non-empty string empty_value is used if placeholder is not set [maintains BC]' => array(false, true, true, null, 'bar', 'bar'),
1812+
'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),
1813+
'A placeholder is not used if empty_value is not set [maintains BC]' => array(false, true, true, '', null, null),
1814+
'An empty string empty_value is converted to "None" in an expanded single choice field [maintains BC]' => array(false, true, true, '', '', 'None'),
1815+
'A non-empty string empty_value is used if placeholder is an empty string [maintains BC]' => array(false, true, true, '', 'bar', 'bar'),
1816+
'A non-empty string placeholder takes precedence over an empty_value set to false' => array(false, true, true, 'foo', false, 'foo'),
1817+
'A non-empty string placeholder takes precendece over a not set empty_value' => array(false, true, true, 'foo', null, 'foo'),
1818+
'A non-empty string placeholder takes precedence over an empty string empty_value' => array(false, true, true, 'foo', '', 'foo'),
1819+
'A non-empty string placeholder takes precedence over a non-empty string empty_value' => array(false, true, true, 'foo', 'bar', 'foo'),
1820+
// multiple expanded, not required
1821+
array(true, true, false, false, false, null),
1822+
array(true, true, false, false, null, null),
1823+
array(true, true, false, false, '', null),
1824+
array(true, true, false, false, 'bar', null),
1825+
array(true, true, false, null, false, null),
1826+
array(true, true, false, null, null, null),
1827+
array(true, true, false, null, '', null),
1828+
array(true, true, false, null, 'bar', null),
1829+
array(true, true, false, '', false, null),
1830+
array(true, true, false, '', null, null),
1831+
array(true, true, false, '', '', null),
1832+
array(true, true, false, '', 'bar', null),
1833+
array(true, true, false, 'foo', false, null),
1834+
array(true, true, false, 'foo', null, null),
1835+
array(true, true, false, 'foo', '', null),
1836+
array(true, true, false, 'foo', 'bar', null),
1837+
// multiple expanded, required
1838+
array(true, true, true, false, false, null),
1839+
array(true, true, true, false, null, null),
1840+
array(true, true, true, false, '', null),
1841+
array(true, true, true, false, 'bar', null),
1842+
array(true, true, true, null, false, null),
1843+
array(true, true, true, null, null, null),
1844+
array(true, true, true, null, '', null),
1845+
array(true, true, true, null, 'bar', null),
1846+
array(true, true, true, '', false, null),
1847+
array(true, true, true, '', null, null),
1848+
array(true, true, true, '', '', null),
1849+
array(true, true, true, '', 'bar', null),
1850+
array(true, true, true, 'foo', false, null),
1851+
array(true, true, true, 'foo', null, null),
1852+
array(true, true, true, 'foo', '', null),
1853+
array(true, true, true, 'foo', 'bar', null),
1854+
);
1855+
}
1856+
17291857
public function testPassChoicesToView()
17301858
{
17311859
$choices = array('A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd');

0 commit comments

Comments
 (0)
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