Use form_validator to check that the email is the same

Issue

I’m using the form_validator Flutter plugin to create a form block.
My form contains contains a lot of fields including Email and Email confirmation:

TextFormField(
  validator: ValidationBuilder()
      .email()
      .maxLength(50)
      .required()
      .build(),
  decoration: const InputDecoration(labelText: 'email'),
),
const SizedBox(height: 30),

TextFormField(
  validator: ValidationBuilder()
      .email()
      .maxLength(50)
      .required()
      .build(),
  decoration:
      const InputDecoration(labelText: 'email confirmation'),
),

They are equals, the only thing that changes is the label. How can I add a check to the second field (EMAIL CONFIRMATION) that controls that it’s the same value of the first one?

Example:

EMAIL: john.snow@gmail.com
EMAIL CONFIRMATION: john.snow@gmail.com
--> ok

EMAIL: john.snow@gmail.com
EMAIL CONFIRMATION: harry.potter@gmail.com
--> error

Solution

You could simply use 2 text controllers like say,

emailTextController

confirmEmailTextController

Make sure you add the following validator method in the Confirm Email TextFormField

validator: (value) {
    if (value != emailTextController.text) {
        return 'Email is not matching';
    }
},

And you get the respective errorText, whose fontStyle can be adjusted.

Answered By – Prudhvik Chirunomula

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *