添加“帮助”到字段的消息
我试图在 symfony2 中的表单中的每个字段后面添加一些帮助消息。
我在官方文档中读到了一种解决方案: http://symfony.com/ doc/current/cookbook/form/form_customization.html#adding-help-messages
但是这个解决方案没有什么意义,因为我们需要手动创建所有表单。 例如,定义标签很容易: $formBuilder->add('myfieldname', 'text', array('label'=>'some my field label'));
但是如何传递帮助消息? (也就是说,一些自定义变量)
I'm trying to add some help messages after each field in form in symfony2.
I have read about one solution in official docs : http://symfony.com/doc/current/cookbook/form/form_customization.html#adding-help-messages
But this solution makes little sense, because we've need to create all form manually.
For example, it easy to define label: $formBuilder->add('myfieldname', 'text', array('label'=>'some my field label'));
But how to pass help messages? (In other words, some custom variables)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有点偏离主题,但如果您计划在项目中使用 Bootstrap,那么您可以利用 Mopa Bootstrap Bundle 提供的一些表单助手,但仍然有用。
演示:http://bootstrap.mohrenweiserpartner.de/mopa/bootstrap/forms/help_texts< /a>
GitHub:https://github.com/phiamo/MopaBootstrapBundle
示例:
A little off topic but still useful if you're planning to use Bootstrap for your project then you can take advantage of some form helpers provided by the Mopa Bootstrap Bundle.
Demo: http://bootstrap.mohrenweiserpartner.de/mopa/bootstrap/forms/help_texts
GitHub: https://github.com/phiamo/MopaBootstrapBundle
Example:
没有其他扩展的另一种方法:
在您的表单构建器类中:
在您的表单模板重写中:
A another method without another extension :
In your form builder class:
In your form template rewrite:
$formBuilder->add('myFieldName', 'text', array('help' => 'My Help Message'));
但它认为您还需要添加一个扩展来添加这是所有表单的默认选项:https://github.com/simplethings/SimpleThingsFormExtraBundle#helpextension
这使您能够直接从 FormType 编辑属性。
$formBuilder->add('myFieldName', 'text', array('help' => 'My Help Message'));
But it think you also need to add an extension that add this as a default option for all forms :https://github.com/simplethings/SimpleThingsFormExtraBundle#helpextension
This makes you able to edit attributes directly from you FormTypes.
从 symfony 4.1 开始,您可以执行以下操作:
https://symfony。 com/blog/new-in-symfony-4-1-form-field-help
Since symfony 4.1 you can do :
https://symfony.com/blog/new-in-symfony-4-1-form-field-help
您可以按照您的描述使用官方文档中的解决方案。
但是,工作还没有完成。您必须根据本文创建表单类型扩展: http://symfony .com/doc/current/cookbook/form/create_form_type_extension.html
完成表单类型扩展创建后,您可以添加如下帮助消息:
我认为这是一个原生的更好的解决方案。
另外,我建议阅读这篇精彩的文章,它向您展示了如何在 symfony2 表单中启用和设置帮助选项:
http://toni .uebernickel.info/2012/11/03/how-to-extend-form-fields-in-symfony2.1.html
You can use the solution in the official docs as you described.
But, the work is not complete yet. You have to create a Form Type Extention, based on this article: http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html
After complete the Form Type Extention creation you can add Help Messages like this:
I think this is a native better solution.
Also, i recommend read this great article that shows you how to enable and set the help option in symfony2 forms:
http://toni.uebernickel.info/2012/11/03/how-to-extend-form-fields-in-symfony2.1.html