Skip to content

Commit 745ee6b

Browse files
committed
fix-17579
1 parent 8f3c06b commit 745ee6b

File tree

7 files changed

+72
-4
lines changed

7 files changed

+72
-4
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@
8787
{% endif %}
8888
{%- endblock time_widget %}
8989

90+
{% block choice_widget -%}
91+
{% if empty_choices is not defined -%}
92+
{{- parent() -}}
93+
{%- else -%}
94+
{% set empty_choices = 'No choice available' %}
95+
{% set choice_translation_domain = 'sf_form' %}
96+
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-control')|trim}) %}
97+
<div {{ block('widget_container_attributes') }}>
98+
{{- choice_translation_domain is same as(false) ? empty_choices : empty_choices|trans({}, choice_translation_domain) -}}
99+
</div>
100+
{%- endif %}
101+
{%- endblock %}
102+
90103
{% block choice_widget_collapsed -%}
91104
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-control')|trim}) %}
92105
{{- parent() -}}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,20 @@
3535
{%- endblock textarea_widget -%}
3636

3737
{%- block choice_widget -%}
38-
{% if expanded %}
39-
{{- block('choice_widget_expanded') -}}
40-
{% else %}
41-
{{- block('choice_widget_collapsed') -}}
38+
{% if empty_choices is not defined -%}
39+
{%- if expanded -%}
40+
{{- block('choice_widget_expanded') -}}
41+
{%- else -%}
42+
{{- block('choice_widget_collapsed') -}}
43+
{%- endif -%}
44+
{%- else -%}
45+
{%- if empty_choices is empty -%}
46+
{% set empty_choices = 'No choice available' %}
47+
{% set choice_translation_domain = 'sf_form' %}
48+
{%- endif -%}
49+
<div {{ block('widget_container_attributes') }}>
50+
{{- choice_translation_domain is same as(false) ? empty_choices : empty_choices|trans({}, choice_translation_domain) -}}
51+
</div>
4252
{% endif %}
4353
{%- endblock choice_widget -%}
4454

src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@
171171
<li>{{ form_widget(child, {
172172
parent_label_class: label_attr.class|default(''),
173173
}) }}</li>
174+
{% else -%}
175+
<li>{{ empty_choices|raw }}</li>
174176
{% endfor %}
175177
</ul>
176178
{% else %}
@@ -179,6 +181,8 @@
179181
{{ form_widget(child, {
180182
parent_label_class: label_attr.class|default(''),
181183
}) }}
184+
{% else -%}
185+
<p>{{ empty_choices|raw }}</p>
182186
{% endfor %}
183187
</div>
184188
{% endif %}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
<?php if (false !== isset($empty_choices)): ?>
12
<?php if ($expanded): ?>
23
<?php echo $view['form']->block($form, 'choice_widget_expanded') ?>
34
<?php else: ?>
45
<?php echo $view['form']->block($form, 'choice_widget_collapsed') ?>
56
<?php endif ?>
7+
<?php else: ?>
8+
<?php if ('' === $empty_choices) {
9+
$empty_choices = 'No choice available';
10+
$choice_translation_domain = 'sf_form';
11+
}
12+
if (false !== $choice_translation_domain) {
13+
echo $view['translator']->trans($empty_choices, array(), $choice_translation_domain);
14+
} else {
15+
echo $view->escape($empty_choices);
16+
} ?>
17+
<?php endif ?>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ public function buildView(FormView $view, FormInterface $form, array $options)
219219
// POST request.
220220
$view->vars['full_name'] .= '[]';
221221
}
222+
223+
// Provide a default message when choices var is empty
224+
if (array() === $view->vars['choices'] && array() === $view->vars['preferred_choices']) {
225+
$view->vars['empty_choices'] = null !== $options['empty_choices'] ? $options['empty_choices'] : '';
226+
}
222227
}
223228

224229
/**
@@ -315,6 +320,7 @@ public function configureOptions(OptionsResolver $resolver)
315320
'preferred_choices' => array(),
316321
'group_by' => null,
317322
'empty_data' => $emptyData,
323+
'empty_choices' => null,
318324
'placeholder' => $placeholderDefault,
319325
'error_bubbling' => false,
320326
'compound' => $compound,
@@ -330,6 +336,7 @@ public function configureOptions(OptionsResolver $resolver)
330336
$resolver->setNormalizer('choices_as_values', $choicesAsValuesNormalizer);
331337

332338
$resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable'));
339+
$resolver->setAllowedTypes('empty_choices', array('null', 'string'));
333340
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
334341
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'));
335342
$resolver->setAllowedTypes('choice_label', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
3+
<file source-language="en" datatype="plaintext" original="file.ext">
4+
<body>
5+
<trans-unit id="1">
6+
<source>No choice available</source>
7+
<target><em>No choice available.</em></target>
8+
</trans-unit>
9+
</body>
10+
</file>
11+
</xliff>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
3+
<file source-language="en" datatype="plaintext" original="file.ext">
4+
<body>
5+
<trans-unit id="1">
6+
<source>No choice available</source>
7+
<target><em>Aucun choix n'est disponible.</em></target>
8+
</trans-unit>
9+
</body>
10+
</file>
11+
</xliff>

0 commit comments

Comments
 (0)