【问题标题】:Symfony 3 dynamically add attributes to form fields based on translation messagesSymfony 3 根据翻译消息动态添加属性到表单字段
【发布时间】:2017-05-16 06:58:36
【问题描述】:

我想为每个表单元素实现自定义帮助消息(html 数据属性)。帮助消息在 YAML 语言文件 (messages.en.yml) 中定义,并非所有表单元素都有帮助消息。

问题是我不确定是否可以使用 Symfony FormType 或 Symfony Form 事件来完成。

我正在考虑将表单事件创建为服务并注入翻译服务,然后从那里操作数据并添加帮助程序类,但我没有找到任何可靠的示例如何做到这一点。

我正在考虑的其他选择是使用 Trait 并在 trait 中注入翻译服务,然后从那里开始开发我的代码,但感觉不太对。

有人可以分享他们的经验和线索如何解决这个特定问题吗?

这是我的设置

messages.en.yml

intro:
        created: Intro created!
        edited:  Intro has been edited successfully!
        deleted: Intro has been has been deleted!
        index:
            title: List of all intros
        new:
            title: New intro
        show:
            title: Details of intro
        edit:
            title: Edit intro
        form:
             title: Title
             content: Content
             isEnabled: Is active
        tooltip:
             title: Please enter title

我的表单类型:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', TextType::class, array(
                'label_format' => 'admin.intro.form.%name%',
                'attr' => [
                    'data-helper' => 'Please enter title'
                ]

            )
        )
        ->add('content', TextareaType::class, array(
                'label_format' => 'admin.intro.form.%name%',
                'required' => false,
                'attr' => [
                    'class' => 'mceEditor'
                ]
            )
        )
        ->add('isEnabled', CheckboxType::class, array(
                'label_format' => 'admin.intro.form.%name%',
                'required' => false,
            )

【问题讨论】:

    标签: php forms symfony symfony-3.1


    【解决方案1】:

    您可以通过registering the form as a serviceYAML configuration 注入到表单中。

    config.yml

    message_config:
        intro:
                created: Intro created!
                edited:  Intro has been edited successfully!
                deleted: Intro has been has been deleted!
                index:
                    title: List of all intros
                new:
                    title: New intro
                show:
                    title: Details of intro
                edit:
                    title: Edit intro
                form:
                     title: Title
                     content: Content
                     isEnabled: Is active
                tooltip:
                     title: Please enter title
    

    services.yml

    services:
    app.form.type.my_form:
        class: AppBundle\Form\Type\MyForm
        arguments:
            - '%message_config%'
        tags:
            - { name: form.type }
    

    现在您可以使用 FormType 中的数组了。

    <?php
    
    namespace AppBundle\Form\Type;
    
    class MyForm
    {
        protected $messages;
    
        public function __construct(array $messages)
        {
            $this->messages = $messages;
        }
    }
    

    如果 YAML 配置正在被 Translator 服务使用,您将改为注入翻译器服务。

    更新:Symfony 使用 Translator 组件和 Twig 表单主题执行此操作,请参阅以下评论。

    【讨论】:

    • 感谢您的帮助。我可以对所有表单执行相同的配置吗?
    • 如果您在整个应用程序中都需要类似的东西,我建议您看看 Symfony 的翻译组件并应用它的最佳实践。 symfony.com/doc/current/translation.html
    • 这实际上应该可以解决您的问题。您可以注册 Translation Twig Extension 并将翻译直接添加到您的 Twig 表单主题中。使用此解决方案,每个表格都会被翻译,您不必注入翻译器。 symfony.com/doc/current/components/form.html#translation
    • 感谢您的建议。我使用了 FormType Extension,它完全匹配。感谢您的帮助。
    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    相关资源
    最近更新 更多