如何将 css 类添加到特定的 symfony2 表单选项? [英] How can I add css classes to specific symfony2 form choices?

查看:23
本文介绍了如何将 css 类添加到特定的 symfony2 表单选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用 Javascript 做到这一点,但我想知道是否可以将 css 类添加到特定的 symfony2 表单选项(不是选项字段本身,而是单个选项).

I could do this with Javascript, but I was wondering if I could add a css class to specific symfony2 form choices (not the choice field itself, but the individual choices).

例如,我想将不同的 css 样式应用于select"中的各个option"标签.我只能找到一种向标签添加类的方法.

For example I want to apply different css styles to individual 'option' tags inside a 'select'. I could only find a way to add a class to the tag.

提前致谢.

推荐答案

您可以覆盖表单中特定小部件的布局,这意味着您可以覆盖选择呈现的方式并放入自定义代码以检查其值选项是并在那里输出您的自定义类.

You can override the layout of specific widgets in your form, which means you can override the way the select renders and put in custom code to check what the value of the option is and output your custom class there.

您需要对表单应用自定义布局,就像这样

You need to apply a custom layout to your form, like so

{% form_theme form 'form_theme.html.twig' %}

然后在布局文件中,您需要覆盖该特定表单的特定字段(当然,除非您想直接编辑choice_widget,在这种情况下,所有使用choice 的字段都将具有该功能).

Then inside the layout file you need to override the specific field for that specific form (unless of course you want to edit the choice_widget directly in which case all fields that use choice will have the functionality).

要做到这一点,您必须复制小部件,因此选择_widget,然后将其命名为[_formName_fieldName_widget]

To do that you have to copy the widget, so choice_widget, then name it [_formName_fieldName_widget]

因此,如果您的表单名称是 events 而您的字段名称是 requireTickets,那么它应该是 _events_requireTickets_widget

So if your form name was events and your field name was requireTickets, it'd be _events_requireTickets_widget

这篇关于如何将 css 类添加到特定的 symfony2 表单选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆