Symfony 5.3 - FormBuilder RepeatedType - 当密码不同时,错误消息不会显示在表单中

Posted

技术标签:

【中文标题】Symfony 5.3 - FormBuilder RepeatedType - 当密码不同时,错误消息不会显示在表单中【英文标题】:Symfony 5.3 - FormBuilder RepeatedType - Error message is not shown in form when password not the same 【发布时间】:2021-09-21 02:29:50 【问题描述】:

我有一个带有 RepeatedTyp 密码的表单。

            ->add('password', RepeatedType::class, [
            'type' => PasswordType::class,
            'label' => 'form.register.password.label',
            'label_attr' => ['class' => 'text-blue'],
            'invalid_message' => 'The passwords do not match!',
            'first_options' => [
                'attr' => ['placeholder' => 'form.register.password.opt1.placeholder', 'class' => 'form-control']
            ],
            'second_options' => [
                'attr' => ['placeholder' => 'form.register.password.opt2.placeholder', 'class' => 'mt-1 form-control']]
        ])

在 Twig 中看起来像这样:

 form_label(register.password) <span class="text-danger">*</span><br>
 form_widget(register.password.first) <br>
 form_widget(register.password.second) 

如果我输入两个不同的密码值并提交表单,我会在分析器工具栏中看到错误消息,但它并未显示在表单本身中。

在 Symfony 文档中声明“如果两个字段不匹配,则会向用户显示错误”。 https://symfony.com/doc/current/reference/forms/types/repeated.html#validation

我该怎么做才能显示消息或更好,我做错了什么?

非常感谢您的帮助!

非常感谢!

马库斯

【问题讨论】:

【参考方案1】:

好吧,看来我只是累了。

忘记了 form_errors(register.password)

对不起。

【讨论】:

以上是关于Symfony 5.3 - FormBuilder RepeatedType - 当密码不同时,错误消息不会显示在表单中的主要内容,如果未能解决你的问题,请参考以下文章