Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Form validation in flutter passes despite not meeting conditions

I am building a Flutter application with multiple TextFormFields. I create a reusable TextFormfield Widget to keep things modular. The problem is, when the submit button is clicked, even though the Text Form Fields are not valid, it runs like it is valid.

My TextFormField widget:

class AndroidTextField extends StatelessWidget {
  final ValueChanged<String> onChanged;
  final String Function(String?)? validator;
  const AndroidTextField(
      {Key? key,
      required this.onChanged,
      this.validator})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      width: size.width * .9,
      child: TextFormField(
        validator: validator,
        onChanged: onChanged,
    );
  }
}

How I use it in the Scaffold

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                        AndroidTextField(
                            validator: (value) {
                              if (value == null || value.isEmpty) {
                                return 'Enter a valid name';
                              }
                              return '';
                            },
                            onChanged: (val) {
                              setState(() {
                                lastName = val;
                              });
                            }),

The form

  final _formKey = GlobalKey<FormState>();
  @override
   Widget build(BuildContext context) {
    return Scaffold(
     body: Form(
        key: _formKey, 
        AndroidTextField(
            validator: (value) {
               if (value == null || value.isEmpty) {
               return 'Enter a valid name';
               }
               return '';
          },
            onChanged: (val) {
            setState(() {
            firstName = val;
            });
            }),

       TextButton(
         child: Text('Press),
         onPressed:(){
            if (_formKey.currentState!.validate()){
                  //do something
            }else{
                 //don't do the something
            }

        }
      ));

     }

>Solution :

I feel a flutter validator should return null if valid, not an empty String.

So the code should be:

AndroidTextField(
 validator: (value) {
   if (value == null || value.isEmpty) {
     return 'Enter a valid name';
   }
   return null;
 }
...

Also, try:

final String? Function(String?) validator;

instead of

final String Function(String?)? validator;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading