Php 如何在Symfony2 FormType中添加自定义类以选择标记

Php 如何在Symfony2 FormType中添加自定义类以选择标记,php,html,forms,symfony,twig,Php,Html,Forms,Symfony,Twig,我需要将自定义类添加到由带有symfony2的表单类型生成的select标记()中。 所有选项均由Symfony生成 $builder->add( 'birthday', null, array( 'label' => 'form.birthday' , 'translation_domain' => 'FOSUserBundle' ) ) 生成代码为: <div id="fos_user_registrat

我需要将自定义类添加到由带有symfony2的
表单类型生成的select标记(
)中。 所有选项均由Symfony生成

$builder->add(
    'birthday',
    null,
    array(
        'label' => 'form.birthday'
       , 'translation_domain' => 'FOSUserBundle'
    )
)
生成代码为:

<div id="fos_user_registration_form_birthday" placeholder="Mot de passe" class="form-control">
  <select id="fos_user_registration_form_birthday_day" name="fos_user_registration_form[birthday][day]">
    <option value="1">1</option>
    ...
  </select>
  <select id="fos_user_registration_form_birthday_month" name="fos_user_registration_form[birthday][month]">
    <option value="1">jan</option>
    ...
  </select>
  <select id="fos_user_registration_form_birthday_year"    name="fos_user_registration_form[birthday][year]">
    <option value="1994">1994</option>
    ...
  </select>
</div>
但是
test
类被添加到我的div(
)中,而不是添加到我的
select
标记中


有什么建议吗?

在表单类中,可以使用attr选项设置类。 您可以在表单类型中完成,也可以稍后在模板中完成,就像刚才一样

它可能不使用您的“测试”类,因为生日小部件不使用它。请在整个项目中搜索“生日小部件”,看看它是否使用硬编码的表单控件类。如果是,您可以编写一条If语句,检查attr.class是否为definer或添加如下内容:

{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}
编辑:你也可以尝试分别渲染每天、月份和年份(我不确定这是否有效,因为我不知道生日类型),就像这样

form_widget(form.birthday.day, {'attr': {'class': 'test'}})
您可以尝试的另一件事是编辑表单类型

在添加方法后尝试添加此项:

$builder->get('birthday')->get('day')->setAttribute('class', 'test'); // Also try with ->get('days')
我几乎可以肯定上面的方法会奏效。

看看这个
form_widget(form.birthday.day, {'attr': {'class': 'test'}})
$builder->get('birthday')->get('day')->setAttribute('class', 'test'); // Also try with ->get('days')