Skip to content

Commit

Permalink
using radio/checkbox instead of expanded widget
Browse files Browse the repository at this point in the history
  • Loading branch information
bamarni committed Jul 5, 2015
1 parent aacafe8 commit ecd5df3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 82 deletions.
24 changes: 16 additions & 8 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Expand Up @@ -55,7 +55,9 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ('expanded' == $options['widget']) {
if (in_array($options['widget'], array('radio', 'checkbox'))) {
$options['multiple'] = 'checkbox' == $options['widget'];

$builder->setDataMapper($options['multiple']
? new CheckboxListMapper($options['choice_list'])
: new RadioListMapper($options['choice_list']));
Expand Down Expand Up @@ -178,7 +180,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars = array_replace($view->vars, array(
'widget' => $options['widget'],
'multiple' => $options['multiple'],
'expanded' => $options['expanded'], // BC
'expanded' => in_array($options['widget'], array('radio', 'checkbox')), // BC
'preferred_choices' => $choiceListView->preferredChoices,
'choices' => $choiceListView->choices,
'separator' => '-------------------',
Expand Down Expand Up @@ -225,7 +227,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
*/
public function finishView(FormView $view, FormInterface $form, array $options)
{
if ('expanded' == $options['widget']) {
if (in_array($options['widget'], array('radio', 'checkbox'))) {
// Radio buttons should have the same name as the parent
$childName = $view->vars['full_name'];

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

$widget = function (Options $options) {
return $options['expanded'] ? 'expanded' : 'select';
if ($options['expanded']) {
return $options['multiple'] ? 'checkbox' : 'radio';
}

return 'select';
};

$emptyData = function (Options $options) {
if ($options['multiple'] || $options['expanded']) {
if ($options['multiple'] || 'checkbox' == $options['widget']) {
return array();
}

Expand Down Expand Up @@ -304,7 +310,7 @@ public function configureOptions(OptionsResolver $resolver)
} elseif (false === $placeholder) {
// an empty value should be added but the user decided otherwise
return;
} elseif ($options['expanded'] && '' === $placeholder) {
} elseif ('radio' == $options['widget'] && '' === $placeholder) {
// never use an empty label for radio buttons
return 'None';
}
Expand All @@ -314,7 +320,7 @@ public function configureOptions(OptionsResolver $resolver)
};

$compound = function (Options $options) {
return 'expanded' == $options['widget'];
return in_array($options['widget'], array('radio', 'checkbox'));
};

$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
Expand Down Expand Up @@ -368,6 +374,8 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('choice_attr', array('null', 'array', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
$resolver->setAllowedTypes('preferred_choices', array('array', '\Traversable', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
$resolver->setAllowedTypes('group_by', array('null', 'array', '\Traversable', 'string', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));

$resolver->setAllowedValues('widget', array('select', 'radio', 'checkbox', 'text', 'hidden'));
}

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

/**
* Adds the sub fields for an expanded choice field.
* Adds the sub fields for a radio/checkbox choice field.
*
* @param FormBuilderInterface $builder The form builder.
* @param array $choiceViews The choice view objects.
Expand Down

0 comments on commit ecd5df3

Please sign in to comment.