【问题标题】:How to translate messages in form Symfony 5.2如何以 Symfony 5.2 的形式翻译消息
【发布时间】:2021-01-29 13:11:29
【问题描述】:

如何在 Symfony 5.2 的 FormType 中为消息设置翻译?我有 validators.fr.yaml 文件,但我不知道如何设置它

->add('plainPassword', PasswordType::class, [
                'mapped' => false,
                'constraints' => [
                    new NotBlank([
                        'message' => 'Please enter a password',
                    ]),
                    new Length([
                        'min' => self::MIN_LENGTH,
                        'minMessage' => 'Your password should be at least {{ limit }} characters',
                        'max' => self::MAX_LENGTH,
                    ]),
                ],
            ])

还有这个

onsubmit="return confirm({{"are you sure?"}}) 

在 form.index.html

【问题讨论】:

    标签: forms symfony translate


    【解决方案1】:

    Symfony 为您处理表单类型句柄翻译。

    将您的翻译密钥添加到validators.fr.yaml

    例子:

    #validators.fr.yaml
    form.password.required: Le mot de passe est obligatoire
    

    然后在您的表单中使用您的翻译键作为您的约束

    new NotBlank([
        'message' => 'form.password.required',
    ])
    

    您可以对表单类型内的所有约束和字段标签执行相同的操作。

    【讨论】:

      猜你喜欢
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多