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

Appearance settings

Commit 4ee2e93

Browse filesBrowse files
committed
feature #12003 [Form] Renamed the option "empty_value" to "placeholder" (webmozart)
This PR was merged into the 2.6-dev branch. Discussion ---------- [Form] Renamed the option "empty_value" to "placeholder" | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #5791 | License | MIT | Doc PR | TODO This PR is changing the "empty_value" option to the more understandable name "placeholder". In a subsequent PR, the "placeholder" option should also be added to all types that support the "placeholder" HTML5 attribute. Commits ------- 2b440f3 [Form] Renamed the option "empty_value" to "placeholder"
2 parents 1b49368 + 2b440f3 commit 4ee2e93
Copy full SHA for 4ee2e93

File tree

Expand file treeCollapse file tree

12 files changed

+319
-170
lines changed
Filter options
Expand file treeCollapse file tree

12 files changed

+319
-170
lines changed

‎UPGRADE-2.6.md

Copy file name to clipboardExpand all lines: UPGRADE-2.6.md
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
11
UPGRADE FROM 2.5 to 2.6
22
=======================
33

4+
Form
5+
----
6+
7+
* The "empty_value" option in the types "choice", "date", "datetime" and "time"
8+
was deprecated and replaced by a new option "placeholder". You should use
9+
the option "placeholder" together with the view variables "placeholder" and
10+
"placeholder_in_choices" now.
11+
12+
The option "empty_value" and the view variables "empty_value" and
13+
"empty_value_in_choices" will be removed in Symfony 3.0.
14+
15+
Before:
16+
17+
```php
18+
$form->add('category', 'choice', array(
19+
'choices' => array('politics', 'media'),
20+
'empty_value' => 'Select a category...',
21+
));
22+
```
23+
24+
After:
25+
26+
```php
27+
$form->add('category', 'choice', array(
28+
'choices' => array('politics', 'media'),
29+
'placeholder' => 'Select a category...',
30+
));
31+
```
32+
33+
Before:
34+
35+
```
36+
{{ form.vars.empty_value }}
37+
38+
{% if form.vars.empty_value_in_choices %}
39+
...
40+
{% endif %}
41+
```
42+
43+
After:
44+
45+
```
46+
{{ form.vars.placeholder }}
47+
48+
{% if form.vars.placeholder_in_choices %}
49+
...
50+
{% endif %}
51+
```
52+
453
Validator
554
---------
655

‎src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@
5252
{% endblock choice_widget_expanded %}
5353

5454
{% block choice_widget_collapsed -%}
55-
{% if required and empty_value is none and not empty_value_in_choices and not multiple -%}
55+
{% if required and placeholder is none and not placeholder_in_choices and not multiple -%}
5656
{% set required = false %}
5757
{%- endif -%}
5858
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
59-
{% if empty_value is not none -%}
60-
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ empty_value|trans({}, translation_domain) }}</option>
59+
{% if placeholder is not none -%}
60+
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder|trans({}, translation_domain) }}</option>
6161
{%- endif %}
6262
{%- if preferred_choices|length > 0 -%}
6363
{% set options = preferred_choices %}

‎src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_collapsed.html.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_collapsed.html.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<select
2-
<?php if ($required && null === $empty_value && $empty_value_in_choices === false && $multiple === false):
2+
<?php if ($required && null === $placeholder && $placeholder_in_choices === false && $multiple === false):
33
$required = false;
44
endif; ?>
55
<?php echo $view['form']->block($form, 'widget_attributes', array(
66
'required' => $required
77
)) ?>
88
<?php if ($multiple): ?> multiple="multiple"<?php endif ?>
99
>
10-
<?php if (null !== $empty_value): ?><option value=""<?php if ($required and empty($value) && "0" !== $value): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($empty_value, array(), $translation_domain)) ?></option><?php endif; ?>
10+
<?php if (null !== $placeholder): ?><option value=""<?php if ($required and empty($value) && "0" !== $value): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($placeholder, array(), $translation_domain)) ?></option><?php endif; ?>
1111
<?php if (count($preferred_choices) > 0): ?>
1212
<?php echo $view['form']->block($form, 'choice_widget_options', array('choices' => $preferred_choices)) ?>
1313
<?php if (count($choices) > 0 && null !== $separator): ?>

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
+23-12Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5757

5858
// Check if the choices already contain the empty value
5959
// Only add the empty value option if this is not the case
60-
if (null !== $options['empty_value'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
61-
$placeholderView = new ChoiceView(null, '', $options['empty_value']);
60+
if (null !== $options['placeholder'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
61+
$placeholderView = new ChoiceView(null, '', $options['placeholder']);
6262

6363
// "placeholder" is a reserved index
6464
// see also ChoiceListInterface::getIndicesForChoices()
@@ -101,7 +101,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
101101
'preferred_choices' => $options['choice_list']->getPreferredViews(),
102102
'choices' => $options['choice_list']->getRemainingViews(),
103103
'separator' => '-------------------',
104-
'empty_value' => null,
104+
'placeholder' => null,
105105
));
106106

107107
// The decision, whether a choice is selected, is potentially done
@@ -119,13 +119,17 @@ public function buildView(FormView $view, FormInterface $form, array $options)
119119
}
120120

121121
// Check if the choices already contain the empty value
122-
$view->vars['empty_value_in_choices'] = 0 !== count($options['choice_list']->getChoicesForValues(array('')));
122+
$view->vars['placeholder_in_choices'] = 0 !== count($options['choice_list']->getChoicesForValues(array('')));
123123

124124
// Only add the empty value option if this is not the case
125-
if (null !== $options['empty_value'] && !$view->vars['empty_value_in_choices']) {
126-
$view->vars['empty_value'] = $options['empty_value'];
125+
if (null !== $options['placeholder'] && !$view->vars['placeholder_in_choices']) {
126+
$view->vars['placeholder'] = $options['placeholder'];
127127
}
128128

129+
// BC
130+
$view->vars['empty_value'] = $view->vars['placeholder'];
131+
$view->vars['empty_value_in_choices'] = $view->vars['placeholder_in_choices'];
132+
129133
if ($options['multiple'] && !$options['expanded']) {
130134
// Add "[]" to the name in case a select tag with multiple options is
131135
// displayed. Otherwise only one of the selected options is sent in the
@@ -187,20 +191,25 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
187191
return $options['required'] ? null : '';
188192
};
189193

190-
$emptyValueNormalizer = function (Options $options, $emptyValue) {
194+
// for BC with the "empty_value" option
195+
$placeholder = function (Options $options) {
196+
return $options['empty_value'];
197+
};
198+
199+
$placeholderNormalizer = function (Options $options, $placeholder) {
191200
if ($options['multiple']) {
192201
// never use an empty value for this case
193202
return;
194-
} elseif (false === $emptyValue) {
203+
} elseif (false === $placeholder) {
195204
// an empty value should be added but the user decided otherwise
196205
return;
197-
} elseif ($options['expanded'] && '' === $emptyValue) {
206+
} elseif ($options['expanded'] && '' === $placeholder) {
198207
// never use an empty label for radio buttons
199208
return 'None';
200209
}
201210

202211
// empty value has been set explicitly
203-
return $emptyValue;
212+
return $placeholder;
204213
};
205214

206215
$compound = function (Options $options) {
@@ -214,7 +223,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
214223
'choices' => array(),
215224
'preferred_choices' => array(),
216225
'empty_data' => $emptyData,
217-
'empty_value' => $emptyValue,
226+
'empty_value' => $emptyValue, // deprecated
227+
'placeholder' => $placeholder,
218228
'error_bubbling' => false,
219229
'compound' => $compound,
220230
// The view data is always a string, even if the "data" option
@@ -224,7 +234,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
224234
));
225235

226236
$resolver->setNormalizers(array(
227-
'empty_value' => $emptyValueNormalizer,
237+
'empty_value' => $placeholderNormalizer,
238+
'placeholder' => $placeholderNormalizer,
228239
));
229240

230241
$resolver->setAllowedTypes(array(

‎src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
115115
'months',
116116
'days',
117117
'empty_value',
118+
'placeholder',
118119
'required',
119120
'translation_domain',
120121
'html5',
@@ -127,6 +128,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
127128
'with_minutes',
128129
'with_seconds',
129130
'empty_value',
131+
'placeholder',
130132
'required',
131133
'translation_domain',
132134
'html5',
@@ -237,7 +239,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
237239
// Don't add some defaults in order to preserve the defaults
238240
// set in DateType and TimeType
239241
$resolver->setOptional(array(
240-
'empty_value',
242+
'empty_value', // deprecated
243+
'placeholder',
241244
'years',
242245
'months',
243246
'days',

‎src/Symfony/Component/Form/Extension/Core/Type/DateType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/DateType.php
+19-13Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
8888
if ('choice' === $options['widget']) {
8989
// Only pass a subset of the options to children
9090
$yearOptions['choices'] = $this->formatTimestamps($formatter, '/y+/', $this->listYears($options['years']));
91-
$yearOptions['empty_value'] = $options['empty_value']['year'];
91+
$yearOptions['placeholder'] = $options['placeholder']['year'];
9292
$monthOptions['choices'] = $this->formatTimestamps($formatter, '/[M|L]+/', $this->listMonths($options['months']));
93-
$monthOptions['empty_value'] = $options['empty_value']['month'];
93+
$monthOptions['placeholder'] = $options['placeholder']['month'];
9494
$dayOptions['choices'] = $this->formatTimestamps($formatter, '/d+/', $this->listDays($options['days']));
95-
$dayOptions['empty_value'] = $options['empty_value']['day'];
95+
$dayOptions['placeholder'] = $options['placeholder']['day'];
9696
}
9797

9898
// Append generic carry-along options
@@ -171,24 +171,28 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
171171
return $options['widget'] !== 'single_text';
172172
};
173173

174-
$emptyValue = $emptyValueDefault = function (Options $options) {
174+
$emptyValue = $placeholderDefault = function (Options $options) {
175175
return $options['required'] ? null : '';
176176
};
177177

178-
$emptyValueNormalizer = function (Options $options, $emptyValue) use ($emptyValueDefault) {
179-
if (is_array($emptyValue)) {
180-
$default = $emptyValueDefault($options);
178+
$placeholder = function (Options $options) {
179+
return $options['empty_value'];
180+
};
181+
182+
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
183+
if (is_array($placeholder)) {
184+
$default = $placeholderDefault($options);
181185

182186
return array_merge(
183187
array('year' => $default, 'month' => $default, 'day' => $default),
184-
$emptyValue
188+
$placeholder
185189
);
186190
}
187191

188192
return array(
189-
'year' => $emptyValue,
190-
'month' => $emptyValue,
191-
'day' => $emptyValue,
193+
'year' => $placeholder,
194+
'month' => $placeholder,
195+
'day' => $placeholder,
192196
);
193197
};
194198

@@ -205,7 +209,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
205209
'format' => $format,
206210
'model_timezone' => null,
207211
'view_timezone' => null,
208-
'empty_value' => $emptyValue,
212+
'empty_value' => $emptyValue, // deprecated
213+
'placeholder' => $placeholder,
209214
'html5' => true,
210215
// Don't modify \DateTime classes by reference, we treat
211216
// them like immutable value objects
@@ -220,7 +225,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
220225
));
221226

222227
$resolver->setNormalizers(array(
223-
'empty_value' => $emptyValueNormalizer,
228+
'empty_value' => $placeholderNormalizer,
229+
'placeholder' => $placeholderNormalizer,
224230
));
225231

226232
$resolver->setAllowedValues(array(

‎src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
+20-13Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
6363

6464
// Only pass a subset of the options to children
6565
$hourOptions['choices'] = $hours;
66-
$hourOptions['empty_value'] = $options['empty_value']['hour'];
66+
$hourOptions['placeholder'] = $options['placeholder']['hour'];
6767

6868
if ($options['with_minutes']) {
6969
foreach ($options['minutes'] as $minute) {
7070
$minutes[$minute] = str_pad($minute, 2, '0', STR_PAD_LEFT);
7171
}
7272

7373
$minuteOptions['choices'] = $minutes;
74-
$minuteOptions['empty_value'] = $options['empty_value']['minute'];
74+
$minuteOptions['placeholder'] = $options['placeholder']['minute'];
7575
}
7676

7777
if ($options['with_seconds']) {
@@ -82,7 +82,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
8282
}
8383

8484
$secondOptions['choices'] = $seconds;
85-
$secondOptions['empty_value'] = $options['empty_value']['second'];
85+
$secondOptions['placeholder'] = $options['placeholder']['second'];
8686
}
8787

8888
// Append generic carry-along options
@@ -163,24 +163,29 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
163163
return $options['widget'] !== 'single_text';
164164
};
165165

166-
$emptyValue = $emptyValueDefault = function (Options $options) {
166+
$emptyValue = $placeholderDefault = function (Options $options) {
167167
return $options['required'] ? null : '';
168168
};
169169

170-
$emptyValueNormalizer = function (Options $options, $emptyValue) use ($emptyValueDefault) {
171-
if (is_array($emptyValue)) {
172-
$default = $emptyValueDefault($options);
170+
// for BC with the "empty_value" option
171+
$placeholder = function (Options $options) {
172+
return $options['empty_value'];
173+
};
174+
175+
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
176+
if (is_array($placeholder)) {
177+
$default = $placeholderDefault($options);
173178

174179
return array_merge(
175180
array('hour' => $default, 'minute' => $default, 'second' => $default),
176-
$emptyValue
181+
$placeholder
177182
);
178183
}
179184

180185
return array(
181-
'hour' => $emptyValue,
182-
'minute' => $emptyValue,
183-
'second' => $emptyValue,
186+
'hour' => $placeholder,
187+
'minute' => $placeholder,
188+
'second' => $placeholder,
184189
);
185190
};
186191

@@ -194,7 +199,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
194199
'with_seconds' => false,
195200
'model_timezone' => null,
196201
'view_timezone' => null,
197-
'empty_value' => $emptyValue,
202+
'empty_value' => $emptyValue, // deprecated
203+
'placeholder' => $placeholder,
198204
'html5' => true,
199205
// Don't modify \DateTime classes by reference, we treat
200206
// them like immutable value objects
@@ -209,7 +215,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
209215
));
210216

211217
$resolver->setNormalizers(array(
212-
'empty_value' => $emptyValueNormalizer,
218+
'empty_value' => $placeholderNormalizer,
219+
'placeholder' => $placeholderNormalizer,
213220
));
214221

215222
$resolver->setAllowedValues(array(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.