Skip to content
Advertisement

Show form errors

I would like to show error messages in the top of my registration form. I created my registration form:

<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>

And on my UserType class, I added all necessaries input:

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(),
         ))
        ;
    }}

Every time I get an error message, correctly displayed, I found it under the concerned input and not in the top of my registration form. I added this on my form but don’t help:

{{ form_errors(form) }}

Any suggestion?

Advertisement

Answer

Did you set error_bubbling => true?

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement