Php Symfony2表单如何设置选项输入字段的类?

Php Symfony2表单如何设置选项输入字段的类?,php,forms,class,symfony,Php,Forms,Class,Symfony,如上所述,如何为输入字段设置类 到目前为止,我得到了什么:form builder类: $builder->add('rate', 'choice', array( 'choices' => array( 1 => 1, 2 => 2, 3 => 3, 4 =>

如上所述,如何为输入字段设置类

到目前为止,我得到了什么:form builder类:

    $builder->add('rate', 'choice', array(
                'choices' => array(
                    1 => 1,
                    2 => 2,
                    3 => 3,
                    4 => 4,
                    5 => 5,
                ),
                'multiple' => false,
                'expanded' => true,
                'attr' => array('disabled' => 'disabled', 'class' => 'star'),
            ))
            ->add('save', 'submit');
所以这里有一句话:

'attr' => array('disabled' => 'disabled', 'class' => 'star'),
为每个输入比率按钮设置潜水而非潜水类

此处显示生成的html:

<form action="/app_dev.php/pl/applications/vote/Google%20Earth%20DirectX" method="post">

    <div id="vote_rate"     disabled="disabled" class="star"><label class="radio"><input type="radio" id="vote_rate_0" name="vote[rate]" required="required"    class="" value="1" />
            1
        </label><label class="radio"><input type="radio" id="vote_rate_1" name="vote[rate]" required="required"    class="" value="2" />
            2
        </label><label class="radio"><input type="radio" id="vote_rate_2" name="vote[rate]" required="required"    class="" value="3" checked="checked" />
            3
        </label><label class="radio"><input type="radio" id="vote_rate_3" name="vote[rate]" required="required"    class="" value="4" />
            4
        </label><label class="radio"><input type="radio" id="vote_rate_4" name="vote[rate]" required="required"    class="" value="5" />
            5
        </label></div>
  <div><button type="submit" id="vote_save" name="vote[save]">Save</button></div><input type="hidden" id="vote__token" name="vote[_token]"    class=" not-removable form-control" value="35bc6584f16d20796946cac15f9e18e62dec3cab" />
</form>

1.
2.
3.
4.
5.
拯救

我们可以看到,我的生成器正在为div
vote\u rate
设置类,我的输入字段有
class=“”
,我希望它是
class=“star”
,我的问题是如何在生成器或其他方式中设置它?

以防在radio小部件中设置输入字段class

在我的表单模板中,我添加了:

{% form_theme form 'ApplicationsBundle:Form:fields.html.twig' %}
这是我的fields.html.twig:

{% block radio_widget %}
{% spaceless %}
<input type="radio" class="star" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} disabled="disabled"/>
{% endspaceless %}
{% endblock radio_widget %}

choice\u attr
选项将允许您向每个选项添加属性(包括类)(在您的示例中为radio)


您可以使用
$val
$key
$index
来确定返回的内容。

一种方法,但不可取,使用jquery遍历所有收音机并添加一个类。使用symfony2没有办法?这将给您一个想法,非常感谢。它确实帮助了我!我下载了radio_小部件,效果非常好!如果把你的问题的解决方案作为答案张贴出来,对未来的访问者会很有帮助
{{ form_widget(form) }}
        'choice_attr' => function($val, $key, $index) {
            return ['disabled' => 'disabled', 'class' => 'star'];
        },