Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to validate a form field based on the value of the other?

Tags:

flutter

dart

I have 2 form fields,I want to validate the second form field to match the password from the first one,I tried but no success..thanks for answering.

Updated : I already have submit button and its working,I want Validator in the second field to validate the first field text to match the second field.

            new TextFormField(
          controller: _registerPassController,
          decoration: new InputDecoration(labelText: 'Password'),
          obscureText: true,
          validator: (value) =>
              value.isEmpty ? 'Password can\'t be empty' : null,
          onSaved: (value) => _password = value,
        ),
      ],
    ),
    new Stack(
      alignment: const Alignment(1.0, 1.0),
      children: <Widget>[
        new TextFormField(
          controller: _registerPassController2,
          decoration: new InputDecoration(labelText: 'Retype Password'),
          obscureText: true,
          validator: (value) {
            if (value.isEmpty) {
              return 'Please enter some text';
            }
          },),
like image 207
ZoRa Avatar asked May 03 '18 12:05

ZoRa


People also ask

How do you check if a field is valid or not?

Using the email type, we can check the validity of the form field with a javascript function called… checkValidity() . This function returns a true|false value. checkValidity() will look at the input type as well as if the required attribute was set and any pattern="" tag .


2 Answers

I finally find the answer,its so simple actually.

        new TextFormField(
          controller: _registerPassController2,
          decoration: new InputDecoration(labelText: 'Retype Password'),
          obscureText: true,
          validator: (value) {
            if (value != _registerPassController.text) {
              return 'Password is not matching';
            }
          },
        ),
like image 189
ZoRa Avatar answered Oct 12 '22 11:10

ZoRa


Since you are using formfield it will be appropriate to use the key to access the value of other field. You can declare the global key like this

var passKey = GlobalKey<FormFieldState>();

Then pass it to the password field to retrieve its value during validation.

TextFormField(
  key: passKey,
  obscureText: true,
  decoration: InputDecoration(
      labelText: "Password"
  ),
  validator: (password){
    var result = password.length < 4 ? "Password should have at least 4 characters" : null;
    return result;
  },
);

Then you can use the password inside the confirmation validator like this

TextFormField(
  obscureText: true,
  decoration: InputDecoration(
      labelText: "Confirm Password"
  ),
  validator: (confirmation){
    var password = passKey.currentState.value;
    return equals(confirmation, password) ? null : "Confirm Password should match password";
  },
);
like image 20
suhair Avatar answered Oct 12 '22 11:10

suhair