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

Skip to content

Commit b133772

Browse files
committed
using radio/checkbox instead of expanded widget
1 parent aacafe8 commit b133772

File tree

3 files changed

+94
-74
lines changed

3 files changed

+94
-74
lines changed

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

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

3737
{%- block choice_widget -%}
38-
{% if 'expanded' == widget %}
39-
{{- block('choice_widget_expanded') -}}
40-
{% elseif 'select' == widget %}
41-
{{- block('choice_widget_collapsed') -}}
42-
{% elseif 'hidden' == widget %}
38+
{% if 'hidden' == widget %}
4339
{{- block('hidden_widget') -}}
44-
{% else %}
40+
{% elseif 'text' == widget %}
4541
{{- block('form_widget_simple') -}}
42+
{% elseif expanded %}
43+
{{- block('choice_widget_expanded') -}}
44+
{% else %}
45+
{{- block('choice_widget_collapsed') -}}
4646
{% endif %}
4747
{%- endblock choice_widget -%}
4848

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
5555
*/
5656
public function buildForm(FormBuilderInterface $builder, array $options)
5757
{
58-
if ('expanded' == $options['widget']) {
58+
if ($options['expanded'] && !in_array($options['widget'], array('text', 'hidden'))) {
5959
$builder->setDataMapper($options['multiple']
6060
? new CheckboxListMapper($options['choice_list'])
6161
: new RadioListMapper($options['choice_list']));
@@ -186,6 +186,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
186186
'choice_translation_domain' => $choiceTranslationDomain,
187187
));
188188

189+
if (in_array($options['widget'], array('text', 'hidden'))) {
190+
return;
191+
}
192+
189193
// The decision, whether a choice is selected, is potentially done
190194
// thousand of times during the rendering of a template. Provide a
191195
// closure here that is optimized for the value of the form, to
@@ -212,7 +216,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
212216
$view->vars['empty_value'] = $view->vars['placeholder'];
213217
$view->vars['empty_value_in_choices'] = $view->vars['placeholder_in_choices'];
214218

215-
if ('select' == $options['widget'] && $options['multiple']) {
219+
if ($options['multiple'] && !$options['expanded']) {
216220
// Add "[]" to the name in case a select tag with multiple options is
217221
// displayed. Otherwise only one of the selected options is sent in the
218222
// POST request.
@@ -225,7 +229,11 @@ public function buildView(FormView $view, FormInterface $form, array $options)
225229
*/
226230
public function finishView(FormView $view, FormInterface $form, array $options)
227231
{
228-
if ('expanded' == $options['widget']) {
232+
if (in_array($options['widget'], array('text', 'hidden'))) {
233+
return;
234+
}
235+
236+
if ($options['expanded']) {
229237
// Radio buttons should have the same name as the parent
230238
$childName = $view->vars['full_name'];
231239

@@ -247,10 +255,6 @@ public function configureOptions(OptionsResolver $resolver)
247255
{
248256
$choiceListFactory = $this->choiceListFactory;
249257

250-
$widget = function (Options $options) {
251-
return $options['expanded'] ? 'expanded' : 'select';
252-
};
253-
254258
$emptyData = function (Options $options) {
255259
if ($options['multiple'] || $options['expanded']) {
256260
return array();
@@ -268,6 +272,18 @@ public function configureOptions(OptionsResolver $resolver)
268272
return $options['empty_value'];
269273
};
270274

275+
$multipleNormalizer = function (Options $options, $multiple) {
276+
if (in_array($options['widget'], array('radio', 'checkbox'))) {
277+
return 'checkbox' == $options['widget'];
278+
}
279+
280+
return $multiple;
281+
};
282+
283+
$expandedNomalizer = function (Options $options, $expanded) {
284+
return in_array($options['widget'], array('radio', 'checkbox')) ?: $expanded;
285+
};
286+
271287
$choiceListNormalizer = function (Options $options, $choiceList) use ($choiceListFactory) {
272288
if ($choiceList) {
273289
@trigger_error('The "choice_list" option is deprecated since version 2.7 and will be removed in 3.0. Use "choice_loader" instead.', E_USER_DEPRECATED);
@@ -314,7 +330,7 @@ public function configureOptions(OptionsResolver $resolver)
314330
};
315331

316332
$compound = function (Options $options) {
317-
return 'expanded' == $options['widget'];
333+
return $options['expanded'];
318334
};
319335

320336
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
@@ -326,7 +342,7 @@ public function configureOptions(OptionsResolver $resolver)
326342
};
327343

328344
$resolver->setDefaults(array(
329-
'widget' => $widget,
345+
'widget' => null,
330346
'multiple' => false,
331347
'delimiter' => ',',
332348
'expanded' => false, // deprecated
@@ -352,6 +368,8 @@ public function configureOptions(OptionsResolver $resolver)
352368
'choice_translation_domain' => true,
353369
));
354370

371+
$resolver->setNormalizer('expanded', $expandedNomalizer);
372+
$resolver->setNormalizer('multiple', $multipleNormalizer);
355373
$resolver->setNormalizer('choice_list', $choiceListNormalizer);
356374
$resolver->setNormalizer('empty_value', $placeholderNormalizer);
357375
$resolver->setNormalizer('placeholder', $placeholderNormalizer);

0 commit comments

Comments
 (0)