Symfony3 - 如何将单选按钮与选择类型分开 [英] Symfony3 - How to separate radio buttons from a choicetype

查看:30
本文介绍了Symfony3 - 如何将单选按钮与选择类型分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个带有单选按钮(输入组)和该字段上方的单个单选按钮的文本字段.我想要链接 2 个单选按钮.

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

<label class="col-sm-4 control-label">字段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 class="form-group"><label class="col-sm-4 control-label">字段2:</label><div class="col-sm-8"><input type="radio" aria-label="..." name="choice" value="f2">

我不知道该怎么做,因为无法操作选择类型中的单选按钮链接不同的字段.或者我可以吗?

解决方案

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

$builder->add('choiceField', ChoiceType::class, array('选择' =>[你的选择]'扩展' =>真的,'多个' =>错误的));$builder->add('textField')

在您的模板中,您需要拆分 choiceField 字段的渲染.您可以:

  1. 在示例中使用直接的 HTML,确保使用 Symfony 表单系统期望的 name 属性以确保验证有效
  2. 在适当的地方使用 Symfony 表单渲染,如下所示:

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

方法 2 是首选方法,因为它将处理表单重新填充.

I would like to create a text field with a radio button (input group) and a single radio button above that field. I want the 2 radio buttons to be linked.

Here is an example : 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>

I don't know how to do that since the radio buttons in a choicetype can't be manipulated link distinct fields. Or can I ?

解决方案

Build your Symfony form class with two fields (the text field + your choice field).

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

In your template, you need to split the rendering of the choiceField field. You can either:

  1. use straight HTML as you have in your example, making sure you use the name attribute expected by the Symfony form system to ensure validation works
  2. use the Symfony form rendering where appropriate, as follows:

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

Method 2 is the preferred one as it will take care of form repopulation.

这篇关于Symfony3 - 如何将单选按钮与选择类型分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆