Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create two related radiobuttons?

Tags:

symfony

The code below creates 2 radiobuttons, however they are not related to each other. One is rendered with a name description_form[friend] and the other one with the name - description_form[guide]. How can they be rendered with the same name? The documentation is not clear about this subject.

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('friend', RadioType::class, array(
                'label'    => 'Friend',
                'required' => false
            ))
            ->add('guide', RadioType::class, array(
                'label'    => 'Guide',
                'required' => false
            ));
    }
like image 311
Alex Lomia Avatar asked Mar 03 '16 19:03

Alex Lomia


People also ask

How do you group radio buttons together?

You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.

Can radio buttons have multiple selections?

Radio buttons allow a user to select a single option among multiple options. You can set the Choice Value of each option, for each button, as well as group these buttons by giving them the same Group Name.

How do you create multiple options buttons in Excel?

If the Controls task pane is not visible, click More Controls on the Insert menu, or press ALT+I, C. Under Insert controls, click Option Button. In the Insert Option Buttons dialog box, enter the number of buttons that you want to insert, and then click OK.

How do I use multiple radio buttons?

For example, create a form using the <form> tag. Then, use the <fieldset> tag for the group of motorcycle radio buttons. Create a radio button for Honda and set the value motorcycle for the name attribute. Next, create another radio button for Yamaha and again set the value of the name attribute to motorcycle .


2 Answers

Using a list of RadioType is not quite easy, that's why everybody recommends you to use a ChoiceType which dynamically creates a radio list depending on an array of choice data.

When you create a FormTypeInterface, it has to represent (commonly) one field or one sub form in a global form, so each field name has to be unique to be mapped to the corresponding data.

The buildForm method allows to add some sub fields in you FormType, in your case the field holds two sub fields as radio button and each has a specific name, this is intended by default, but you should always keep in mind the global array data you want to deal with.

Here's your example :

class MyCustomFormType extends \Symfony\Component\Form\AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('friend', RadioType::class, array(
                'label'    => 'Friend',
                'required' => false
            ))
            ->add('guide', RadioType::class, array(
                'label'    => 'Guide',
                'required' => false
            ));
    }

    public function getBlockPrefix
    {
        return 'my_custom';
    }

    // ...
}

So this form type data should look like :

$myCustomFormData = array(
    'friend' => $friendData,
    'guide' => $guideData,
);

And nested in a global form it would be :

$formData = array(
    'my_custom' => $myCustomFormData,
);

But you can name the field as you want :

// In a controller extending \Symfony\Bundle\FrameworkBundle\Controller\Controller
$form = $this->createFormBuilder()
    ->add('custom_field', MyCustomFormType::class)
    ->getForm();

// Then
$form->setData(array('custom_field' => $myCustomFormData));

Note that currently, since you map "friend" and "guide" data to RadioType they should hold a boolean value as :

$myCustomFormData = array(
    'friend' => true, // checked
    'guide' => false, // unchecked
);

But how would you unselect a value then ?

You should had a placeholder to do that, and handle it while submission...

Also, changing the name can be done using the finishView method of your type class, it takes the FormView (built view of your type), the form itself and the options as arguments :

public function finishView(FormView $view, FormInterface $form, array $options)
{
    $childName = $view->vars['full_name']; // "my_custom" by default

    foreach ($view as $childView) {
        $childView->vars['full_name'] = $childName;
    }
}

But you would also need to add a DataMapperInterface to get back the submitted value to the form type itself instead.

To do all that, you need to know how the Form Component works and it's not easy.

Easy way

So I agree with the other answers, you should use a ChoiceType to get it out-of-the-box.

I assume your custom form type is about choosing either a "friend" or a "guide", so it could look like this :

$builder
    ->add('fellow', ChoiceType::class, array(
        'choices' => array(
            'I have a friend' => 'friend',
            'I\'d like a guide' => 'guide',
        ),
        'expanded' => true, // use a radio list instead of a select input
        // ...

Have a look at the official docs

Then your data will look like :

$form->add('custom_field', MyCustomFormType::class);

$form->setData(array(
    'custom_field' => 'friend',
));

When rendered the "friend" choice will be selected and you will be able to change it to "guide".

An array of choices for the choices options takes labels as keys and choice values as values :

<div id="form_custom_field">
    <input type="radio" name="form[custom_field]" value="friend" checked="checked">
    <label>I have a friend</label> 
    <input type="radio" name="form[custom_field]" value="guide">
    <label>I'd like a guide</label>

... 
like image 137
Heah Avatar answered Oct 19 '22 02:10

Heah


This is how I do radio buttons in Symfony 2.7 , hope it helps you.

   $yes_no = array('1'=>'Yes','0'=>'No');

   ->add('myfieldname', 'choice',array(
        'choices' => $yes_no,
        'label'=>'YourLabelGoeshere',
        'required'=>true,
        'expanded'=>true,
        'multiple'=>false,
        'placeholder'=>false
))
like image 31
Muhammed Avatar answered Oct 19 '22 01:10

Muhammed