Home > OS >  Use form_validator to check that the email is the same
Use form_validator to check that the email is the same

Time:03-23

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: [email protected]
EMAIL CONFIRMATION: [email protected]
--> ok

EMAIL: [email protected]
EMAIL CONFIRMATION: [email protected]
--> error

CodePudding user response:

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.

  • Related