Loc*_*gar 2 symfony-forms symfony
我有我必须用 Symfony Form 重做的表格,但我坚持在这个地方:
<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>
Run Code Online (Sandbox Code Playgroud)
如何使用 Symfony Forms 制作单选按钮?在我的表单类中,我添加了:
->add('vacancyCurrency', RadioType::class, ['required' => false])
Run Code Online (Sandbox Code Playgroud)
和我的模板
{{ form_widget(form.vacancyCurrency) }}
Run Code Online (Sandbox Code Playgroud)
我的表格变短了,显然没有 css :
如果我添加 css 类来形成它看起来不同:
{{ form_widget(form.vacancyCurrency, {'attr':
{'class': 'currency-label'}
}) }}
Run Code Online (Sandbox Code Playgroud)
最好的方法是将 a 呈现ChoiceType
为单选按钮:
->add(
'vacancyCurrency',
ChoiceType::class,
[
'choices' => [
'US Dolar' => 'usd',
'Euro' => 'eur',
],
'expanded' => true
]
);
Run Code Online (Sandbox Code Playgroud)
当您有选项multiple = false
和expanded = true
. 该选项multiple
不在上面的代码中,因为它的默认值为false
。您可以在此处找到更多详细信息
编辑:
在你的树枝模板中,你只需要添加:
{{ form_widget(form.vacancyCurrency) }}
Run Code Online (Sandbox Code Playgroud)
编辑 2
您在对此答案的评论中说,您需要将每个单选按钮放在<div class="currency-label active">
. 我相信您不能通过在 Symfony 表单字段本身中设置属性来做到这一点。您在那里拥有的选项,例如choice_attr
在 中操作,而input
不是在围绕它的 div 中操作。
可以实现您想要的功能,但您需要编写一些代码并手动呈现单选按钮,例如:
<div class="form-group">
<label class="control-label required">Vacancy currency</label>
{% for child in form.vacancyCurrency %}
<div class="currency-label active">
<label for="{{ child.vars.id }}" class="required">
<input type="radio" id="{{ child.vars.id }}" name="{{ form.vars.id ~ '[' ~ form.vacancyCurrency.vars.name ~ ']' }}" required="required" class="checkbox" value="{{ child.vars.label }}">
{{ child.vars.label }}
</label>
</div>
{% endfor %}
</div>
Run Code Online (Sandbox Code Playgroud)
当然,您可以使用一些 Javascript 来完成这项工作。例如,您可以将表单字段呈现为:
{{ form_widget(form.vacancyCurrency) }}
Run Code Online (Sandbox Code Playgroud)
它将在 div 中添加每个单选按钮class="radio"
。
然后,您可以在页面准备好并使用一些 JQuery 将类更改为您想要的:
$(document).ready(function() {
$(".radio").attr("class", "currency-label active");
});
Run Code Online (Sandbox Code Playgroud)
所以这就是我在模板中解决这个问题的方法:
{{ 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) }}
Run Code Online (Sandbox Code Playgroud)
在表单类中:
->add('vacancyCurrency', ChoiceType::class, [
'choices' => [
'USD' => 'USD',
'RUB' => 'RUB',
],
'expanded' => true,
])
Run Code Online (Sandbox Code Playgroud)
为了使其中之一处于活动状态,我在模型中设置了默认数据:
public $vacancyCurrency = 'RUB';
Run Code Online (Sandbox Code Playgroud)
更新
现在我有货币接口并在表单类中使用它:
->add('currency', ChoiceType::class, [
'label' => '??????',
'choices' => \array_combine(CurrencyInterface::ALL, CurrencyInterface::ALL)
])
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3047 次 |
最近记录: |