2016-12-07 57 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

相關問題