How to create two connected radio lenses?

In the code below, 2 radio exchanges are created, however they are not connected to each other. One is displayed with a name description_form[friend], and the other with a name - description_form[guide]. How can they be displayed with the same name? The documentation is not clear on this.

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
            ));
    }
+4
source share
4 answers

Using a list is RadioTypenot so simple, so everyone recommends you use ChoiceTypeone that dynamically creates a list of radio depending on the array of data of choice.

FormTypeInterface () , .

buildForm FormType, , , , .

:

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';
    }

    // ...
}

, :

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

:

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

, :

// 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));

, , "friend" "guide" RadioType, :

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

?

...

, finishView , FormView ( ), :

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;
    }
}

DataMapperInterface, .

, , Form, .

, , ChoiceType, .

, "" "", :

$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
        // ...

:

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

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

"", "".

choices :

<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>

... 
+6

Symfony 2.7, , .

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

   ->add('myfieldname', 'choice',array(
        'choices' => $yes_no,
        'label'=>'YourLabelGoeshere',
        'required'=>true,
        'expanded'=>true,
        'multiple'=>false,
        'placeholder'=>false
))
+2

, ChoiceType.

:

, .

+1

RadioType ChoiceType. ChoiceType.

0

All Articles