如何以 Symfony 形式制作 RadioButton? [英] How to make RadioButton in Symfony Form?

查看:38
本文介绍了如何以 Symfony 形式制作 RadioButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我必须用 Symfony Form 重做的表格,但我坚持在这个地方:

<input checked id="cur1" name="currency" type="radio"><label for="cur1">EURO</label>

<div class="currency-label active"><input id="cur2" name="currency" type="radio"><label for="cur2">USD</label>

这是它的样子:

如何使用 Symfony Forms 制作单选按钮?在我的表单类中,我添加了:

->add('vacancyCurrency', RadioType::class, ['required' => false])

和我的模板

{{ form_widget(form.vacancyCurrency) }}

我的表单变短了,显然没有 css :如果我添加 css 类来形成它看起来不同:

{{ form_widget(form.vacancyCurrency, {'attr':{'class': '货币标签'}}) }}

解决方案

这就是我在模板中解决这个问题的方法:

 {{ form_start(form) }}{% form_theme 表单_self %}{% 块 choice_widget %}{% for child in form.vacancyCurrency %}<div class="currency-label">{{- form_widget(child) -}}{{- form_label(child, null) -}}

{% 结束为 %}{% endblock choice_widget %}{{ form_end(form) }}

在表单类中:

->add('vacancyCurrency', ChoiceType::class, ['选择' =>['美元' =>'美元','擦' =>'擦',],'扩展' =>真的,])

为了使其中一个处于活动状态,我在模型中设置了默认数据:

public $vacancyCurrency = 'RUB';

<块引用>

更新

现在我有货币接口并在表单类中使用它:

->add('currency', ChoiceType::class, ['标签' =>'Валюта','选择' =>\array_combine(CurrencyInterface::ALL, CurrencyInterface::ALL)])

I have form that i have to redo with Symfony Form but i stuck on this place:

<div class="currency-label">
    <input checked id="cur1" name="currency" type="radio">
    <label for="cur1">EURO</label>
</div>
<div class="currency-label active">
    <input id="cur2" name="currency" type="radio">
    <label for="cur2">USD</label>
</div>

This is how it looks:

how to make radiobutton with Symfony Forms? In my form class i added:

->add('vacancyCurrency', RadioType::class, ['required' => false])

and to my template

{{ form_widget(form.vacancyCurrency) }}

my form became shorter and obviously no css : and if i add css class to form it looks differently :

{{ form_widget(form.vacancyCurrency, {'attr':
      {'class': 'currency-label'}
}) }}

解决方案

So that is how i solved this, in template:

 {{ form_start(form) }}
 {% form_theme form _self %}
 {% block choice_widget %}
     {% for child in form.vacancyCurrency %}
         <div class="currency-label">
             {{- form_widget(child) -}}
             {{- form_label(child, null) -}}
         </div>
     {% endfor %}
 {% endblock choice_widget %}
 {{ form_end(form) }}

in form class:

->add('vacancyCurrency', ChoiceType::class, [
    'choices' => [
        'USD' => 'USD',
        'RUB' => 'RUB',
    ],
    'expanded' => true,
])

and to make one of them active i set default data in model:

public $vacancyCurrency = 'RUB';

Update

now i have interface for currency and use it in form class:

->add('currency', ChoiceType::class, [
    'label' => 'Валюта',
    'choices' => \array_combine(CurrencyInterface::ALL, CurrencyInterface::ALL)
])

这篇关于如何以 Symfony 形式制作 RadioButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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