Home > Software engineering >  Invalid message doesn't show when password doesn't match its confirmation
Invalid message doesn't show when password doesn't match its confirmation

Time:01-12

I'm trying to display an error message when password doesn't match its confirmation.

Here is the relevant code in the form class file:

->add('password', RepeatedType::class, array(
            'required' => true,
            'invalid_message' => 'Le mot de passe et sa confirmation ne sont pas identiques',
            'type' => PasswordType::class,
            'first_options' => array('label' => false),
            'second_options' => array('label' => false),
        ))

And here is the relevant Twig code:

<div >
                    <div >
                        {{ form_row(registrationForm.password.first ,{'label':false,'attr':{'placeholder':'Mot de passe', 'name':'password1', 'class':'form-control', 'id':'password1', 'required data-error':'Veuillez saisir votre mot de passe'} } ) }}
                    </div>
                </div>
                <div >
                    <div >
                        {{ form_row(registrationForm.password.second ,{'label':false,'attr':{'placeholder':'Confirmation mot de passe', 'name':'password2', 'class':'form-control', 'id':'password2', 'required data-error':'Veuillez confirmer votre mot de passe'} } ) }}
                    </div>
                </div>

The issue is that when putting different passwords then validating the form, the expected message error doesn't show. Any idea about how to fix that?

CodePudding user response:

I've fixed it! I've changed this line 'first_options' => array('label' => false), to this one 'first_options' => array('label' => false,'error_bubbling' => true), in my form class file. So, its code has been changed like so:

->add('password', RepeatedType::class, array(
        'required' => true,
        'invalid_message' => 'Le mot de passe et sa confirmation ne sont pas identiques',
        'type' => PasswordType::class,
        'first_options' => array('label' => false,'error_bubbling' => true),
        'second_options' => array('label' => false),
    ))

Besides, I've added this line <span style="color: red;text-align: right">{{ form_errors(registrationForm.password) }}</span> in my HTML\Twig code like so:

<div >
     <div >
          {{ form_row(registrationForm.password.first ,{'label':false,'attr':{'placeholder':'Mot de passe', 'name':'password1', 'class':'form-control', 'id':'password1', 'required data-error':'Veuillez saisir votre mot de passe'} } ) }}
          <span  style="color: red;text-align: right">{{ form_errors(registrationForm.password) }}</span>
     </div>
</div>
<div >
    <div >
         {{ form_row(registrationForm.password.second ,{'label':false,'attr':{'placeholder':'Confirmation mot de passe', 'name':'password2', 'class':'form-control', 'id':'password2', 'required data-error':'Veuillez confirmer votre mot de passe'} } ) }}
    </div>
</div>
  •  Tags:  
  • Related