Skip to content

Commit da95b53

Browse files
committed
bug symfony#15945 [Form] trigger deprecation warning when using empty_value (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] trigger deprecation warning when using empty_value | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#15908 | License | MIT | Doc PR | The `empty_value` option is deprecated in the `choice`, `date`, and `time` form types. Therefore, a deprecation warning must be triggered when the users configures a value for this option. The `datetime` form type does not need to be updated as it passes configured values to the `date` and `time` form types which trigger deprecation warnings anyway. Commits ------- 405d4a8 trigger deprecation warning when using empty_value
2 parents aacbcef + 405d4a8 commit da95b53

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,8 @@ public function configureOptions(OptionsResolver $resolver)
248248
return '';
249249
};
250250

251-
$emptyValue = function (Options $options) {
252-
return $options['required'] ? null : '';
253-
};
254-
255-
// for BC with the "empty_value" option
256251
$placeholder = function (Options $options) {
257-
return $options['empty_value'];
252+
return $options['required'] ? null : '';
258253
};
259254

260255
$choiceListNormalizer = function (Options $options, $choiceList) use ($choiceListFactory) {
@@ -287,6 +282,12 @@ public function configureOptions(OptionsResolver $resolver)
287282
};
288283

289284
$placeholderNormalizer = function (Options $options, $placeholder) {
285+
if (!is_object($options['empty_value']) || !$options['empty_value'] instanceof \Exception) {
286+
@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);
287+
288+
$placeholder = $options['empty_value'];
289+
}
290+
290291
if ($options['multiple']) {
291292
// never use an empty value for this case
292293
return;
@@ -328,7 +329,7 @@ public function configureOptions(OptionsResolver $resolver)
328329
'preferred_choices' => array(),
329330
'group_by' => null,
330331
'empty_data' => $emptyData,
331-
'empty_value' => $emptyValue, // deprecated
332+
'empty_value' => new \Exception(), // deprecated
332333
'placeholder' => $placeholder,
333334
'error_bubbling' => false,
334335
'compound' => $compound,
@@ -340,7 +341,6 @@ public function configureOptions(OptionsResolver $resolver)
340341
));
341342

342343
$resolver->setNormalizer('choice_list', $choiceListNormalizer);
343-
$resolver->setNormalizer('empty_value', $placeholderNormalizer);
344344
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
345345
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer);
346346

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,17 @@ public function configureOptions(OptionsResolver $resolver)
175175
return $options['widget'] !== 'single_text';
176176
};
177177

178-
$emptyValue = $placeholderDefault = function (Options $options) {
178+
$placeholder = $placeholderDefault = function (Options $options) {
179179
return $options['required'] ? null : '';
180180
};
181181

182-
$placeholder = function (Options $options) {
183-
return $options['empty_value'];
184-
};
185-
186182
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
183+
if (!is_object($options['empty_value']) || !$options['empty_value'] instanceof \Exception) {
184+
@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);
185+
186+
$placeholder = $options['empty_value'];
187+
}
188+
187189
if (is_array($placeholder)) {
188190
$default = $placeholderDefault($options);
189191

@@ -213,7 +215,7 @@ public function configureOptions(OptionsResolver $resolver)
213215
'format' => $format,
214216
'model_timezone' => null,
215217
'view_timezone' => null,
216-
'empty_value' => $emptyValue, // deprecated
218+
'empty_value' => new \Exception(), // deprecated
217219
'placeholder' => $placeholder,
218220
'html5' => true,
219221
// Don't modify \DateTime classes by reference, we treat
@@ -228,7 +230,6 @@ public function configureOptions(OptionsResolver $resolver)
228230
'compound' => $compound,
229231
));
230232

231-
$resolver->setNormalizer('empty_value', $placeholderNormalizer);
232233
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
233234

234235
$resolver->setAllowedValues('input', array(

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,17 @@ public function configureOptions(OptionsResolver $resolver)
163163
return $options['widget'] !== 'single_text';
164164
};
165165

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

170-
// for BC with the "empty_value" option
171-
$placeholder = function (Options $options) {
172-
return $options['empty_value'];
173-
};
174-
175170
$placeholderNormalizer = function (Options $options, $placeholder) use ($placeholderDefault) {
171+
if (!is_object($options['empty_value']) || !$options['empty_value'] instanceof \Exception) {
172+
@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);
173+
174+
$placeholder = $options['empty_value'];
175+
}
176+
176177
if (is_array($placeholder)) {
177178
$default = $placeholderDefault($options);
178179

@@ -199,7 +200,7 @@ public function configureOptions(OptionsResolver $resolver)
199200
'with_seconds' => false,
200201
'model_timezone' => null,
201202
'view_timezone' => null,
202-
'empty_value' => $emptyValue, // deprecated
203+
'empty_value' => new \Exception(), // deprecated
203204
'placeholder' => $placeholder,
204205
'html5' => true,
205206
// Don't modify \DateTime classes by reference, we treat
@@ -214,7 +215,6 @@ public function configureOptions(OptionsResolver $resolver)
214215
'compound' => $compound,
215216
));
216217

217-
$resolver->setNormalizer('empty_value', $placeholderNormalizer);
218218
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
219219

220220
$resolver->setAllowedValues('input', array(

0 commit comments

Comments
 (0)