向Symfony3添加所有FormTypes的帮助选项(Adding help option to Symfony3 all FormTypes)

编程入门 行业动态 更新时间:2024-10-26 04:29:27
向Symfony3添加所有FormTypes的帮助选项(Adding help option to Symfony3 all FormTypes)

如何为所有现有的Symfony3 Type添加自定义“帮助”选项?

在Symfony2中,我这样做了http://toni.uebernickel.info/2012/11/03/how-to-extend-form-fields-in-symfony2.1.html,但现在我正在升级到Symfony3和它不再起作用 - 它给了我选项“帮助”不存在

http://symfony.com/doc/current/form/form_customization.html#adding-help-messages可以使用,但需要将所有帮助文本移动到模板中:

{{ form_widget(form.title, {'help': 'foobar'}) }}

...来自类型类:

->add( 'periodFrom', TextType::class, [ 'label' => 'period-from', 'required' => false, 'help' => 'period-from.help' ] )

我想避免这种情况。 谢谢。

How could I add custom 'help' option to all existing Symfony3 Type's?

In Symfony2, I did it like this http://toni.uebernickel.info/2012/11/03/how-to-extend-form-fields-in-symfony2.1.html but now I'm upgrading to Symfony3 and it does not work any more - it gives me The option "help" does not exist.

http://symfony.com/doc/current/form/form_customization.html#adding-help-messages would work, but it would require to move all help texts into template:

{{ form_widget(form.title, {'help': 'foobar'}) }}

...from Type classes:

->add( 'periodFrom', TextType::class, [ 'label' => 'period-from', 'required' => false, 'help' => 'period-from.help' ] )

I'd like to avoid that. Thanks.

最满意答案

为此,在此http://symfony.com/doc/current/form/form_customization.html#adding-help-messages之后,您可以创建表单类型扩展,以将help选项传递给所有表单字段:

<?php // src/AppBundle/Form/Extension/FormTypeExtension.php namespace AppBundle\Form\Extension; use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; class FormTypeExtension extends AbstractTypeExtension { public function buildView(FormView $view, FormInterface $form, array $options) { $view->vars['help'] = $options['help']; } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'help' => null, )); } public function getExtendedType() { return FormType::class; } }

现在注册表单类型扩展名:

services: app.form_type_extension: class: AppBundle\Form\Extension\FormTypeExtension tags: - { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType }

To do that and after this http://symfony.com/doc/current/form/form_customization.html#adding-help-messages you can create a form type extension to pass help option to all form fields:

<?php // src/AppBundle/Form/Extension/FormTypeExtension.php namespace AppBundle\Form\Extension; use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; class FormTypeExtension extends AbstractTypeExtension { public function buildView(FormView $view, FormInterface $form, array $options) { $view->vars['help'] = $options['help']; } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'help' => null, )); } public function getExtendedType() { return FormType::class; } }

Now register the form type extension:

services: app.form_type_extension: class: AppBundle\Form\Extension\FormTypeExtension tags: - { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType }

更多推荐

本文发布于:2023-08-07 00:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458958.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项   FormTypes   Adding   option

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!