Only first form field validation is saved in flutter

362 views Asked by At

There are 2 Issues I am facing.

  1. Form only remember value for first Field (here name field) when submit is clicked for the first time.

  2. only full name field gets the validation error border when submit is clicked for the first time.

Note: 1)After this first time, everything works fine. 2)Validator works for all as we can see that the image clearly shows errors for all the fields in the flutter form.

enter image description here

enter image description here

Code in question is as followed:


void _handleRegistration() async {
    FormState formState = _formKey.currentState;
    formState.save();
    if (formState.validate()) {
      // No error in validation
      setState(() {
        _isLoading = true;
      });
...
}

Update: adding TextFormField code:

TextFormField buildCPassFormField() {
    return TextFormField(
      onSaved: (newValue) => cpass = newValue,
      onChanged: (value) {
        if (value.isNotEmpty ||
            value.length >= 6 ||
            pass == cpass && errors["cpass"] != null) {
          setState(() {
            errors["cpass"] = "";
          });
          return "";
        }
        return null;
      },
      validator: (value) {
        if (value.isEmpty || value == null) {
          setState(() {
            errors["cpass"] = kCPassNullError;
          });
          return "";
        } else if (value.length < 6) {
          setState(() {
            errors["cpass"] = kShortPassError;
          });
          return "";
        } else if (value != pass) {
          setState(() {
            errors["cpass"] = kMatchPassError;
          });
          return "";
        }
        setState(() {
          cpass = value;
        });
        return null;
      },
      obscureText: true,
      keyboardType: TextInputType.text,
      autofillHints: [AutofillHints.password],
      decoration: InputDecoration(
        floatingLabelBehavior: FloatingLabelBehavior.always,
        suffixIcon:
            CustomSurffixIcon(svgIcon: "assets/icons/md-lock-closed.svg"),
        labelText: 'Confirm Password',
      ),
    );
  }

  TextFormField buildPassFormField() {
    return TextFormField(
      onSaved: (newValue) => pass = newValue,
      onChanged: (value) {
        if (value.isNotEmpty || value.length > 6 && errors["pass"] != null) {
          setState(() {
            errors["pass"] = "";
          });
          return "";
        }
        return null;
      },
      validator: (value) {
        if (value.isEmpty || value == null) {
          setState(() {
            errors["pass"] = kPassNullError;
          });
          return "";
        } else if (value.length < 6) {
          setState(() {
            errors["pass"] = kShortPassError;
          });
          return "";
        }
        setState(() {
          pass = value;
        });
        return null;
      },
      obscureText: true,
      keyboardType: TextInputType.text,
      autofillHints: [AutofillHints.password],
      decoration: InputDecoration(
        floatingLabelBehavior: FloatingLabelBehavior.always,
        suffixIcon:
            CustomSurffixIcon(svgIcon: "assets/icons/md-lock-closed.svg"),
        labelText: 'Password',
      ),
    );
  }

  TextFormField buildEmailFormField() {
    return TextFormField(
      onSaved: (newValue) => email = newValue,
      onChanged: (value) {
        if (value.isNotEmpty ||
            value != null && !emailValidatorRegExp.hasMatch(value)) {
          setState(() {
            errors["email"] = "";
          });
          return "";
        }
        setState(() {
          email = value;
        });
        return null;
      },
      validator: (value) {
        if (value.isEmpty || value == null) {
          setState(() {
            errors["email"] = kEmailNullError;
          });
          return "";
        } else if (!emailValidatorRegExp.hasMatch(value)) {
          setState(() {
            errors["email"] = kInvalidEmailError;
          });
          return "";
        }
        return null;
      },
      autofocus: true,
      autofillHints: [AutofillHints.email],
      keyboardType: TextInputType.emailAddress,
      decoration: InputDecoration(
        floatingLabelBehavior: FloatingLabelBehavior.always,
        suffixIcon: CustomSurffixIcon(svgIcon: "assets/icons/md-mail.svg"),
        labelText: 'Email ID',
      ),
    );
  }

  TextFormField buildNameFormField() {
    return TextFormField(
      onSaved: (newValue) => name = newValue,
      onChanged: (value) {
        if (value.isNotEmpty || nameValidatorRegExp.hasMatch(value)) {
          setState(() {
            errors["name"] = "";
          });
        }
        setState(() {
          name = value;
        });
        return null;
      },
      validator: (value) {
        if (value.isEmpty || value == null) {
          setState(() {
            errors["name"] = kNameNullError;
          });
          return "";
        } else if (!nameValidatorRegExp.hasMatch(value)) {
          setState(() {
            errors["name"] = kInvalidNameError;
          });
          return "";
        }
        return null;
      },
      autofocus: true,
      autofillHints: [AutofillHints.name],
      keyboardType: TextInputType.text,
      decoration: InputDecoration(
        floatingLabelBehavior: FloatingLabelBehavior.always,
        suffixIcon: CustomSurffixIcon(svgIcon: "assets/icons/md-user.svg"),
        labelText: 'Your Full Name',
      ),
    );
  }

0

There are 0 answers