html - 如何将 CSS ID 属性设置为 Symfony2 表单输入

标签 html symfony formbuilder

这个问题类似于another question .那里the solution设置 CSS 类是将其添加到调用 FormBuilder::add() 的第三个参数中:

->add('title', null, array('attr' => array('class'=>'span2')))

不幸的是,这不适用于设置 CSS id。当我做的时候

->add('title', null, array('attr' => array('id'=>'title-field')))

... 这被忽略了。 ID 仍然类似于 namespace_formtype_field。

如果有的话,我该如何设置 CSS ID?

最佳答案

当你在 twig 中渲染你的字段时,你可以这样做,如果你将你的 id 设置在 'attributes' 数组之外,如下所示:

{{ form_widget(form.field, { 'id': 'my_custom_id',  'attr': { 'class' : 'my_custom_class }} )}}

关于html - 如何将 CSS ID 属性设置为 Symfony2 表单输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17070763/

相关文章:

postgresql - Pommbundle 与另一个表的关系

javascript - Yii Framework formbuilder - 有没有办法改变验证javascript中的inputID?

Symfony2 Doctrine : redefine annotation in a inheritance class from another bundle

unit-testing - 在 Jasmine 中通过 @input 模拟父 FormGroup

angular - 无法绑定(bind)到 'ngFormModel',因为它不是已知的 native 属性,在更新 Angular2 rc3 之后

html - 将 margin-left 和 right 设置为 auto 的 <div> 变得不居中 >1634px

html - 使用 BeautifulSoup 通过标签类迭代 html

javascript - 当用户将鼠标悬停在 src 链接地址上时,将其从 png 更改为 gif

python - 为什么当我通过 FastAPI-mail 发送电子邮件时,我收到的电子邮件显示相同的消息两次?

php - Symfony 2 中的集合主题,每个级别的嵌套形式都不同