Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94f2a70

Browse files
committed
[From] add option empty_view to FormType
1 parent 06eb52c commit 94f2a70

File tree

16 files changed

+271
-14
lines changed

16 files changed

+271
-14
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@
232232
</div>
233233
{%- endblock radio_row %}
234234

235+
{# Support #}
236+
237+
{% block empty_row -%}
238+
{%- set translation_domain = translation_domain ?: 'sf_form' -%}
239+
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-control')|trim}) %}
240+
<div {{ block('widget_container_attributes') }}>
241+
<p>
242+
<em>{{- translation_domain is same as(false) ? empty_view : empty_view|trans({}, translation_domain) -}}</em>
243+
</p>
244+
</div>
245+
{%- endblock empty_row %}
246+
235247
{# Errors #}
236248

237249
{% block form_errors -%}

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
{%- block form_widget -%}
44
{% if compound %}
5-
{{- block('form_widget_compound') -}}
5+
{%- if empty_view %}
6+
{{- block('empty_row') -}}
7+
{{ form_rest(form) }}
8+
{%- else -%}
9+
{{- block('form_widget_compound') -}}
10+
{%- endif -%}
611
{% else %}
712
{{- block('form_widget_simple') -}}
813
{% endif %}
@@ -35,11 +40,15 @@
3540
{%- endblock textarea_widget -%}
3641

3742
{%- block choice_widget -%}
38-
{% if expanded %}
39-
{{- block('choice_widget_expanded') -}}
40-
{% else %}
41-
{{- block('choice_widget_collapsed') -}}
42-
{% endif %}
43+
{%- if empty_view is defined and empty_view -%}
44+
{{- block('empty_row') -}}
45+
{%- else -%}
46+
{%- if expanded -%}
47+
{{- block('choice_widget_expanded') -}}
48+
{%- else -%}
49+
{{- block('choice_widget_collapsed') -}}
50+
{%- endif -%}
51+
{%- endif -%}
4352
{%- endblock choice_widget -%}
4453

4554
{%- block choice_widget_expanded -%}
@@ -313,6 +322,13 @@
313322
{% endfor %}
314323
{%- endblock form_rows -%}
315324

325+
{%- block empty_row -%}
326+
{%- set translation_domain = translation_domain ?: 'sf_form' -%}
327+
<div {{ block('widget_container_attributes') }}>
328+
<em>{{- translation_domain is same as(false) ? empty_view : empty_view|trans({}, translation_domain) -}}</em>
329+
</div>
330+
{%- endblock empty_row -%}
331+
316332
{%- block widget_attributes -%}
317333
id="{{ id }}" name="{{ full_name }}"
318334
{%- if disabled %} disabled="disabled"{% endif -%}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,14 @@
4242
{{- form_rest(form) -}}
4343
</table>
4444
{%- endblock form_widget_compound -%}
45+
46+
{%- block empty_row -%}
47+
{%- set translation_domain = translation_domain ?: 'sf_form' -%}
48+
<table {{ block('widget_container_attributes') }}>
49+
<tr>
50+
<td colspan="2">
51+
<em>{{ translation_domain is same as(false) ? empty_view : empty_view|trans({}, translation_domain) }}</em>
52+
</td>
53+
</tr>
54+
</table>
55+
{%- endblock empty_row -%}

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: 7 additions & 3 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') ?>
1+
<?php if (isset($empty_view) && $empty_view): ?>
2+
<?php echo $view['form']->block($form, 'empty_row')?>
33
<?php else: ?>
4-
<?php echo $view['form']->block($form, 'choice_widget_collapsed') ?>
4+
<?php if ($expanded): ?>
5+
<?php echo $view['form']->block($form, 'choice_widget_expanded') ?>
6+
<?php else: ?>
7+
<?php echo $view['form']->block($form, 'choice_widget_collapsed') ?>
8+
<?php endif ?>
59
<?php endif ?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php $translation_domain = $translation_domain ?: 'sf_form'; ?>
2+
<div <?php echo $view['form']->block($form, 'widget_container_attributes') ?>>
3+
<em>
4+
<?php echo $view->escape(false !== $translation_domain
5+
? $view['translator']->trans($empty_view, array(), $translation_domain)
6+
: $empty_view
7+
); ?>
8+
</em>
9+
</div>
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php if ($compound): ?>
2-
<?php echo $view['form']->block($form, 'form_widget_compound')?>
2+
<?php if (isset($empty_view) && $empty_view): ?>
3+
<?php echo $view['form']->block($form, 'empty_row')?>
4+
<?php echo $view['form']->rest($form) ?>
5+
<?php else: ?>
6+
<?php echo $view['form']->block($form, 'form_widget_compound')?>
7+
<?php endif ?>
38
<?php else: ?>
49
<?php echo $view['form']->block($form, 'form_widget_simple')?>
510
<?php endif ?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php $translation_domain = $translation_domain ?: 'sf_form'; ?>
2+
<table <?php echo $view['form']->block($form, 'widget_container_attributes') ?>>
3+
<tr>
4+
<td colspan="2">
5+
<em>
6+
<?php echo $view->escape(false !== $translation_domain
7+
? $view['translator']->trans($empty_view, array(), $translation_domain)
8+
: $empty_view
9+
); ?>
10+
</em>
11+
</td>
12+
</tr>
13+
</table>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,18 @@ public function finishView(FormView $view, FormInterface $form, array $options)
244244
$childView->vars['full_name'] = $childName;
245245
}
246246
}
247+
248+
// We have to override parent {@link FormType} condition of the empty_view option here,
249+
// even if there is no choices the field view may not be empty because:
250+
//
251+
// - a select input is not "compound" but should be considered as any empty HTML tag,
252+
//
253+
// - when expanded it could hold a placeholder which has been counted as a children,
254+
// we should ignore it in any case since its role is to be a "null" alternative to
255+
// choices, not a choice itself. Think of "Choose something" with no choices.
256+
if (array() === $view->vars['choices'] && array() === $view->vars['preferred_choices']) {
257+
$view->vars['empty_view'] = $options['empty_view'];
258+
}
247259
}
248260

249261
/**
@@ -323,6 +335,7 @@ public function configureOptions(OptionsResolver $resolver)
323335
'preferred_choices' => array(),
324336
'group_by' => null,
325337
'empty_data' => $emptyData,
338+
'empty_view' => 'No choice available',
326339
'placeholder' => $placeholderDefault,
327340
'error_bubbling' => false,
328341
'compound' => $compound,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public function finishView(FormView $view, FormInterface $form, array $options)
113113
}
114114

115115
$view->vars['multipart'] = $multipart;
116+
117+
if ($form->getConfig()->getCompound()) {
118+
$view->vars['empty_view'] = (bool) $view->count() ? false : $options['empty_view'];
119+
}
116120
}
117121

118122
/**
@@ -142,6 +146,12 @@ public function configureOptions(OptionsResolver $resolver)
142146
};
143147
};
144148

149+
// For any compound form which has no children, show some text
150+
// instead of an empty HTML tag by default
151+
$emptyView = function (Options $options) {
152+
return $options['compound'] ? 'No fields' : false;
153+
};
154+
145155
// For any form that is not represented by a single HTML control,
146156
// errors should bubble up by default
147157
$errorBubbling = function (Options $options) {
@@ -157,6 +167,7 @@ public function configureOptions(OptionsResolver $resolver)
157167
$resolver->setDefaults(array(
158168
'data_class' => $dataClass,
159169
'empty_data' => $emptyData,
170+
'empty_view' => $emptyView,
160171
'trim' => true,
161172
'required' => true,
162173
'property_path' => null,
@@ -175,6 +186,7 @@ public function configureOptions(OptionsResolver $resolver)
175186
));
176187

177188
$resolver->setAllowedTypes('label_attr', 'array');
189+
$resolver->setAllowedTypes('empty_view', array('null', 'string', 'bool'));
178190
}
179191

180192
/**

0 commit comments

Comments
 (0)