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,
);
, :
$form = $this->createFormBuilder()
->add('custom_field', MyCustomFormType::class)
->getForm();
$form->setData(array('custom_field' => $myCustomFormData));
, , "friend" "guide" RadioType, :
$myCustomFormData = array(
'friend' => true,
'guide' => false,
);
?
...
, finishView , FormView ( ), :
public function finishView(FormView $view, FormInterface $form, array $options)
{
$childName = $view->vars['full_name'];
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,
:
$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>
...