显示表格错误

时间:2019-11-03 14:35:14

标签: php symfony symfony4

我想在我的注册表上方显示错误消息。我创建了注册表格:

<div class="example-wrapper">
<h1>Register</h1>
{{ form_start(form) }}
    {{ form_row(form.email) }}
    {{ form_row(form.plainPassword.first) }}
    {{ form_row(form.plainPassword.second) }}
    {{ form_row(form.firstname) }}
    {{ form_row(form.lastname) }}
    {{ form_row(form.termsAccepted) }}
    <button type="submit">Register!</button>
{{ form_end(form) }}
</div>

在我的UserType类上,我添加了所有必需的输入:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
       ->add('email', EmailType::class)
       ->add('plainPassword', RepeatedType::class, array(
            'type' => PasswordType::class,
            'first_options' => array('label' => 'Password'),
            'second_options' => array('label' => 'Repeat Password'),
            'constraints' => [
                new NotBlank([
                    'message' => 'Enter a valid password.'
                ]),
                new Length([
                    'min' => 8,
                    'minMessage' => 'Password must be at least 8 characters.'
                ])
            ]
         ))
         ->add('firstname', TextType::class)
         ->add('lastname', TextType::class)
         ->add('termsAccepted', CheckboxType::class, array(
              'mapped' => false,
              'constraints' => new IsTrue(),
         ))
        ;
    }}

每次我得到一条错误消息,正确显示时,我都会在相关输入下而不是在我的注册表上方找到它。 我在表单上添加了此内容,但没有帮助:

{{ form_errors(form) }}

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

您设置了error_bubbling => true吗?

文档:https://symfony.com/doc/current/reference/forms/types/text.html#error-bubbling