8000 [From] add option `empty_choices` to `ChoiceType` · symfony/symfony@adef8c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit adef8c4

Browse files
committed
[From] add option empty_choices to ChoiceType
1 parent 697bac9 commit adef8c4

File tree

10 files changed

+169
-8
lines changed

10 files changed

+169
-8
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
@@ -114,6 +114,19 @@
114114
{%- endif %}
115115
{%- endblock choice_widget_expanded %}
116116

117+
{% block choice_widget_empty -%}
118+
{%- if empty_choices is empty %}
119+
{%- set empty_choices = 'No choice available' %}
120+
{%- set choice_translation_domain = 'sf_form' %}
121+
{%- endif -%}
122+
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-control')|trim}) %}
123+
<div {{ block('widget_container_attributes') }}>
124+
<p>
125+
{{- choice_translation_domain is same as(false) ? empty_choices : empty_choices|trans({}, choice_translation_domain) -}}
126+
</p>
127+
</div>
128+
{%- endblock %}
129+
117130
{% block checkbox_widget -%}
118131
{%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
119132
{% if 'checkbox-inline' in parent_label_class %}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@
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 choices is not empty or preferred_choices is not empty -%}
39+
{%- if expanded -%}
40+
{{- block('choice_widget_expanded') -}}
41+
{%- else -%}
42+
{{- block('choice_widget_collapsed') -}}
43+
{%- endif -%}
44+
{%- elseif empty_choices is defined -%}
45+
{{- block('choice_widget_empty') -}}
4246
{% endif %}
4347
{%- endblock choice_widget -%}
4448

@@ -71,6 +75,16 @@
7175
</select>
7276
{%- endblock choice_widget_collapsed -%}
7377

78+
{%- block choice_widget_empty -%}
79+
{%- if empty_choices is empty -%}
80+
{%- set empty_choices = 'No choice available' -%}
81+
{%- set choice_translation_domain = 'sf_form' -%}
82+
{%- endif -%}
83+
<div {{ block('widget_container_attributes') }}>
84+
{{- choice_translation_domain is same as(false) ? empty_choices : empty_choices|trans({}, choice_translation_domain) -}}
85+
</div>
86+
{%- endblock choice_widget_empty -%}
87+
7488
{%- block choice_widget_options -%}
7589
{% for group_label, choice in options %}
7690
{%- if choice is iterable -%}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@
184184
{% endif %}
185185
{%- endblock choice_widget_expanded %}
186186

187+
{% block choice_widget_empty -%}
188+
{%- if empty_choices is empty %}
189+
{%- set empty_choices = 'No choice available' %}
190+
{%- set choice_translation_domain = 'sf_form' %}
191+
{%- endif -%}
192+
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-control')|trim}) %}
193+
<div {{ block('widget_container_attributes') }}>
194+
<p>
195+
{{- choice_translation_domain is same as(false) ? empty_choices : empty_choices|trans({}, choice_translation_domain) -}}
196+
</p>
197+
</div>
198+
{%- endblock %}
199+
187200
{% block checkbox_widget -%}
188201
{% set parent_label_class = parent_label_class|default('') %}
189202
{% if errors|length > 0 -%}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
<?php if ($expanded): ?>
2-
<?php echo $view['form']->block($form, 'choice_widget_expanded') ?>
3-
<?php else: ?>
4-
<?php echo $view['form']->block($form, 'choice_widget_collapsed') ?>
1+
<?php if (0 < count($choices) || 0 < count($preferred_choices)): ?>
2+
<?php if ($expanded): ?>
3+
<?php echo $view['form']->block($form, 'choice_widget_expanded') ?>
4+
<?php else: ?>
5+
<?php echo $view['form']->block($form, 'choice_widget_collapsed') ?>
6+
<?php endif ?>
7+
<?php elseif (isset($empty_choices)): ?>
8+
<?php echo $view['form']->block($form, 'choice_widget_empty'); ?>
59
<?php endif ?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php if ('' === $empty_choices) {
2+
$empty_choices = 'No choice available';
3+
$choice_translation_domain = 'sf_form';
4+
} ?>
5+
<div <?php echo $view['form']->block($form, 'widget_container_attributes') ?>>
6+
<?php echo $view->escape(false !== $choice_translation_domain
7+
? $view['translator']->trans($empty_choices, array(), $choice_translation_domain)
8+
: $empty_choices
9+
); ?>
10+
</div>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ public function buildView(FormView $view, FormInterface $form, array $options)
224224
// POST request.
225225
$view->vars['full_name'] .= '[]';
226226
}
227+
228+
// If choices are empty set an alternative string which defaults to "null" casted to an empty string.
229+
// It should be handled in the layout and is translated with "choice_translation_domain" if any.
230+
// In default form themes it results with the string "<em>No choice available</em>".
231+
if (array() === $view->vars['choices'] && array() === $view->vars['preferred_choices']) {
232+
$view->vars['empty_choices'] = (string) $options['empty_choices'];
233+
}
227234
}
228235

229236
/**
@@ -323,6 +330,7 @@ public function configureOptions(OptionsResolver $resolver)
323330
'preferred_choices' => array(),
324331
'group_by' => null,
325332
'empty_data' => $emptyData,
333+
'empty_choices' => null,
326334
'placeholder' => $placeholderDefault,
327335
'error_bubbling' => false,
328336
'compound' => $compound,
@@ -338,6 +346,7 @@ public function configureOptions(OptionsResolver $resolver)
338346
$resolver->setNormalizer('choices_as_values', $choicesAsValuesNormalizer);
339347

340348
$resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable'));
349+
$resolver->setAllowedTypes('empty_choices', array('null', 'string'));
341350
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
342351
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'));
343352
$resolver->setAllowedTypes('choice_label', array('null', 'bool', '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>![CDATA[<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>![CDATA[<em>Aucun choix n'est disponible.</em>]]</target>
8+
</trans-unit>
9+
</body>
10+
</file>
11+
</xliff>

src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,27 @@ public function testSelectWithSizeBiggerThanOneCanBeRequired()
250250
);
251251
}
252252

253+
public function testSingleChoiceWithoutChoices()
254+
{
255+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array(
256+
'choices' => array(),
257+
'multiple' => false,
258+
'expanded' => false,
259+
'required' => true,
260+
'attr' => array('class' => 'my&class'),
261+
));
262+
263+
$this->assertMatchesXpath($this->renderWidget($form->createView()),
264+
'/div
265+
[@class="my&class form-control"]
266+
[not(@required)]
267+
[
268+
./p[.="[trans]No choice available[/trans]"]
269+
]
270+
'
271+
);
272+
}
273+
253274
public function testSingleChoiceWithoutTranslation()
254275
{
255276
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
@@ -813,6 +834,27 @@ public function testSingleChoiceExpandedWithLabelsSetFalseByCallable()
813834
);
814835
}
815836

837+
public function testSingleChoiceExpandedWithoutChoices()
838+
{
839+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array(
840+
'choices' => array(),
841+
'multiple' => false,
842+
'expanded' => true,
843+
'required' => true,
844+
'attr' => array('class' => 'my&class'),
845+
));
846+
847+
$this->assertMatchesXpath($this->renderWidget($form->createView()),
848+
'/div
849+
[@class="my&class form-control"]
850+
[not(@required)]
851+
[
852+
./p[.="[trans]No choice available[/trans]"]
853+
]
854+
'
855+
);
856+
}
857+
816858
public function testSingleChoiceExpandedWithoutTranslation()
817859
{
818860
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,23 @@ public function testSelectWithSizeBiggerThanOneCanBeRequired()
536536
);
537537
}
538538

539+
public function testSingleChoiceWithoutChoices()
540+
{
541+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array(
542+
'choices' => array(),
543+
'multiple' => false,
544+
'expanded' => false,
545+
'required' => true,
546+
));
547+
548+
$this->assertWidgetMatchesXpath($form->createView(), array(),
549+
'/div
550+
[not(@required)]
551+
[.="[trans]No choice available[/trans]"]
552+
'
553+
);
554+
}
555+
539556
public function testSingleChoiceWithoutTranslation()
540557
{
541558
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
@@ -955,6 +972,23 @@ public function testSingleChoiceExpanded()
955972
);
956973
}
957974

975+
public function testSingleChoiceExpandedWithoutChoices()
976+
{
977+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array(
978+
'choices' => array(),
979+
'multiple' => false,
980+
'expanded' => true,
981+
'required' => true,
982+
));
983+
984+
$this->assertWidgetMatchesXpath($form->createView(), array(),
985+
'/div
986+
[not(@required)]
987+
[.="[trans]No choice available[/trans]"]
988+
'
989+
);
990+
}
991+
958992
public function testSingleChoiceExpandedWithoutTranslation()
959993
{
960994
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(

0 commit comments

Comments
 (0)
0