2016-03-03 49 views
3

下面的代码创建2个单选按钮,但它们彼此不相关。其中一个名称为description_form[friend],另一个名称为 - description_form[guide]。他们怎样才能以同样的名字呈现? documentation不清楚这个问题。如何创建两个相关的单选按钮?

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 
      )); 
    } 
+2

使用的选择,扩大了真,多假。 ChoiceType :: class –

+0

然后RadioType有什么意义? –

回答

4

使用的RadioType列表不是很容易的,这就是为什么每个人都建议你使用ChoiceType其动态创建取决于选择的数据阵列中的电台列表。

当您创建FormTypeInterface时,它必须以全局形式表示(通常)一个字段或一个子表单,因此每个字段名称必须是唯一的以映射到相应的数据。

buildForm方法允许添加一些子领域中,你FormType,在你的情况下,现场举行了2个个子场的单选按钮,每个人都有一个特定的名字,这是默认意图,但要始终牢记全局数组数据,你想处理。

这里是你的榜样:

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

注意,目前,因为你映射“朋友”和“指南”数据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找回提交值表单类型本身,而不是。

要做到这一点,您需要知道表单组件的工作原理,这并不容易。

简单的方法

所以我跟其他的答案同意,你应该使用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 
     // ... 

看一看the official docs

那么你的数据看起来像:

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

... 
+0

谢谢你的回答。看来,“ChoiceType”确实是最好的解决方案。但我不明白,为什么'RadioType'甚至存在呢? –

+1

'ChoiceType'是使用它的实现的一个例子。但是你可以根据From组件来编写任何东西,这就是它的美。 – Heah

+1

'RadioType'只是扩展一个'CheckboxType'并表示相应的html输入。他们帮助处理布尔值。 – Heah

1

也许考虑使用ChoiceType字段。

在这里看到:Documentation

这样您就可以输出选项的单选按钮,如果你选择。

+0

然后RadioType有什么意义?我有困难定制ChoiceType风格 –

2

这是我如何做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 
)) 
相关问题