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

Skip to content

Commit 31591d9

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

File tree

3 files changed

+72
-83
lines changed

3 files changed

+72
-83
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
{%- endblock textarea_widget -%}
3636

3737
{%- block choice_widget -%}
38-
{% if 'expanded' == widget %}
38+
{% if widget in ['radio', 'checkbox'] %}
3939
{{- block('choice_widget_expanded') -}}
4040
{% elseif 'select' == widget %}
4141
{{- block('choice_widget_collapsed') -}}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
5555
*/
5656
public function buildForm(FormBuilderInterface $builder, array $options)
5757
{
58-
if ('expanded' == $options['widget']) {
58+
if (in_array($options['widget'], array('radio', 'checkbox'))) {
59+
$options['multiple'] = 'checkbox' == $options['widget'];
60+
5961
$builder->setDataMapper($options['multiple']
6062
? new CheckboxListMapper($options['choice_list'])
6163
: new RadioListMapper($options['choice_list']));
@@ -178,7 +180,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
178180
$view->vars = array_replace($view->vars, array(
179181
'widget' => $options['widget'],
180182
'multiple' => $options['multiple'],
181-
'expanded' => $options['expanded'], // BC
183+
'expanded' => in_array($options['widget'], array('radio', 'checkbox')), // BC
182184
'preferred_choices' => $choiceListView->preferredChoices,
183185
'choices' => $choiceListView->choices,
184186
'separator' => '-------------------',
@@ -225,7 +227,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
225227
*/
226228
public function finishView(FormView $view, FormInterface $form, array $options)
227229
{
228-
if ('expanded' == $options['widget']) {
230+
if (in_array($options['widget'], array('radio', 'checkbox'))) {
229231
// Radio buttons should have the same name as the parent
230232
$childName = $view->vars['full_name'];
231233

@@ -248,11 +250,15 @@ public function configureOptions(OptionsResolver $resolver)
248250
$choiceListFactory = $this->choiceListFactory;
249251

250252
$widget = function (Options $options) {
251-
return $options['expanded'] ? 'expanded' : 'select';
253+
if ($options['expanded']) {
254+
return $options['multiple'] ? 'checkbox' : 'radio';
255+
}
256+
257+
return 'select';
252258
};
253259

254260
$emptyData = function (Options $options) {
255-
if ($options['multiple'] || $options['expanded']) {
261+
if ($options['multiple'] || 'checkbox' == $options['widget']) {
256262
return array();
257263
}
258264

@@ -304,7 +310,7 @@ public function configureOptions(OptionsResolver $resolver)
304310
} elseif (false === $placeholder) {
305311
// an empty value should be added but the user decided otherwise
306312
return;
307-
} elseif ($options['expanded'] && '' === $placeholder) {
313+
} elseif ('radio' == $options['widget'] && '' === $placeholder) {
308314
// never use an empty label for radio buttons
309315
return 'None';
310316
}
@@ -314,7 +320,7 @@ public function configureOptions(OptionsResolver $resolver)
314320
};
315321

316322
$compound = function (Options $options) {
317-
return 'expanded' == $options['widget'];
323+
return in_array($options['widget'], array('radio', 'checkbox'));
318324
};
319325

320326
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
@@ -368,6 +374,8 @@ public function configureOptions(OptionsResolver $resolver)
368374
$resolver->setAllowedTypes('choice_attr', array('null', 'array', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
369375
$resolver->setAllowedTypes('preferred_choices', array('array', '\Traversable', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
370376
$resolver->setAllowedTypes('group_by', array('null', 'array', '\Traversable', 'string', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
377+
378+
$resolver->setAllowedValues('widget', array('select', 'radio', 'checkbox', 'text', 'hidden'));
371379
}
372380

373381
/**
@@ -394,7 +402,7 @@ private static function flipRecursive($choices, &$output = array())
394402
}
395403

396404
/**
397-
* Adds the sub fields for an expanded choice field.
405+
* Adds the sub fields for a radio/checkbox choice field.
398406
*
399407
* @param FormBuilderInterface $builder The form builder.
400408
* @param array $choiceViews The choice view objects.

0 commit comments

Comments
 (0)