简体   繁体   English

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

[英]How to add a custom class to select tag in Symfony2 FormType

I need to add a custom class to my select tag ( <select> ) generated by an FormType with symfony2. 我需要将自定义类添加到由带有symfony2的FormType生成的选择标记( <select> )中。 All options are generated by Symfony 所有选项均由Symfony生成

$builder->add(
    'birthday',
    null,
    array(
        'label' => 'form.birthday'
       , 'translation_domain' => 'FOSUserBundle'
    )
)

and the generate code is: 生成的代码是:

<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>

And, I need to add a custom class to this: 而且,我需要为此添加一个自定义类:

<select id="fos_user_registration_form_birthday_day" name="fos_user_registration_form[birthday][day]">

I've tried this solution in my html content: 我在我的html内容中尝试了此解决方案:

{{ form_widget(form.birthday, {'attr': {'placeholder': 'form.password'|trans, 'class': 'test'}}) }}

But the test class is added to my div ( <div id="fos_user_registration_form_birthday" placeholder="Mot de passe" class="form-control"></div> ), not to my select tag. 但是将test类添加到我的div( <div id="fos_user_registration_form_birthday" placeholder="Mot de passe" class="form-control"></div> )中,而不是添加到我的select标签中。

Any suggestions? 有什么建议么?

In your Form class, you can set the class using the attr option. 在您的Form类中,可以使用attr选项设置类。 You can already do it inside your Form Type, or later in your template, like you just did. 您已经可以在“表单类型”内部或稍后在模板中进行此操作,就像您刚才所做的那样。

It PROBABLY does not use your 'test' class, because the birthday widget does not use it. 它可能不使用您的“测试”类,因为生日小部件不使用它。 Please search your whole project for "birthday_widget" and see if it uses a hardcoded form-control class. 请在整个项目中搜索“ birthday_widget”,并查看其是否使用硬编码的表单控件类。 If it does, you can write an if statement checking if attr.class is definer or add something like this: 如果是这样,您可以编写一条if语句来检查attr.class是否是定义者,或添加如下内容:

{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}

Edit: Can you also try rendering each day,month and year separately (I'm not sure if that will work, because I do not know the birthday type), like so... 编辑:您还可以尝试分别渲染每天,每月和每年(我不确定是否可以,因为我不知道生日类型),像这样...

form_widget(form.birthday.day, {'attr': {'class': 'test'}})

Another thing you can try is to edit your Form Type. 您可以尝试的另一件事是编辑表单类型。

Try to add this after you add method: 添加方法后,尝试添加以下内容:

$builder->get('birthday')->get('day')->setAttribute('class', 'test'); // Also try with ->get('days')

I'm almost sure something above will work. 我几乎可以肯定上面的内容会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM