Skip to content

Commit 4271ab1

Browse files
committed
[Form] Prefer placeholder option to empty_value
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.
1 parent 779ab76 commit 4271ab1

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
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: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,140 @@ public function getOptionsWithPlaceholder()
17261726
);
17271727
}
17281728

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