Twig上form_widget选项的占位符 [英] Placeholder for form_widget choices on Twig
本文介绍了Twig上form_widget选项的占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在控制器端使用PHP,我可以使用createFormBuilder()在我的字段中添加一个占位符:
Using PHP on controller side, i can add a placeholder to my field using the createFormBuilder():
$form = $this->createFormBuilder($company)
->add('name', 'text')
->add('cities', 'entity', array(
'class' => 'NexBaseBundle:City',
'property' => 'name',
'placeholder' => 'Choose an option',
))
->getForm();
我希望使用Twig添加一个占位符,对于简单的字段,我可以像这样使用attr:
I am looking to add a placeholder using Twig, for simple fields i can use attr like this :
{{ form_widget(form.name, {'attr': {'placeholder': 'My placeholder'} }) }}
我尝试过但没有运气:
{{ form_widget(form.cities, {'attr': {'placeholder': 'Choose your city'} }) }}
推荐答案
您不能因为html5上的选择框"没有属性"placeholder",而在formbuilder中使用属性"placeholder"替代了 empty_value
You can't because the "select box" on html5 does not have an attribute "placeholder", your attribute "placeholder" in your formbuilder is an alternative of empty_value
https://github.com/symfony/symfony/issues/5791
这篇关于Twig上form_widget选项的占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文