2016-12-07 37 views
1

分离我想创建一个文本字段与单选按钮(输入组的)和字段上方的单个单选按钮。我想要连接2个单选按钮。Symfony3 - 如何单选按钮从choicetype

下面是一个例子:https://jsfiddle.net/y8tecoyf/

<div class="form-group"> 
    <label class="col-sm-4 control-label">Field 1 :</label> 
    <div class="col-sm-8 "> 
    <div class="input-group"> 
     <span class="input-group-addon"> 
     <input type="radio" aria-label="..." name="choice" value="f1"> 
     </span> 
     <input type="text" class="form-control" aria-label="..."> 
    </div><!-- /input-group --> 
    </div> 
</div> 
<div class="form-group"> 
    <label class="col-sm-4 control-label">Field 2 :</label> 
    <div class="col-sm-8 "> 
    <input type="radio" aria-label="..." name="choice" value="f2"> 
    </div> 
</div> 

我不知道怎么做,因为在choicetype单选按钮不能被操纵的链接的不同字段。或者我可以吗?

回答

1

构建两个字段(文本字段+您的选择字段)您的Symfony表单类。

$builder->add('choiceField', ChoiceType::class, array(
    'choices' => [your choices] 
    'expanded' => true, 
    'multiple' => false 
)); 
$builder->add('textField') 

在您的模板中,您需要拆分choiceField字段的呈现。您可以:当你在你的榜样,确保您使用name属性由Symfony的形式系统应确保验证

  1. 使用直HTML工作
  2. 使用Symfony的形式呈现在适当情况下,如如下:

    {{ form_widget(form.choiceField[0]) }} 
    {{ form_widget(form.textField) }} 
    {{ form_widget(form.choiceField[1]) }} 
    

方法2是因为它会照顾形式再增殖的优选一个。

+0

嗯,我必须说,袭击现场,我不知道我能,很容易分开choicefields。非常感谢 ! – ThEBiShOp