Home > OS >  Validation in Flutter
Validation in Flutter

Time:12-14

I want to know how to make a validation in flutter where IF a TextFormField is filled then when you hit "send" then it doesn't let you go to the next section until all other textformfields must be filled, BUT if one of the TextFormFields is NOT filled when you hit send then it lets you pass to the next section. This is for a job form where a section is NOT mandatory, but only if one field has been filled then it becomes mandatory.

CodePudding user response:

If you use a TextEditingController you can use the .text.isNotEmpty statement an write yourself a litte if function to check everything.

  TextEditingController controller = TextEditingController();


 if (controller.text.isNotEmpty) {
print("have fun with your new job")                         
  }

CodePudding user response:

If you have a Form widget that contains all your FormFields (not only text-ones, but also dropdowns and such), the validation occurs on all your fields at once if you write your submit code this way:

final _formKey = GlobalKey<FormState>();   
var tecUser = TextEditingController();  
var tecPwd = TextEditingController();  
[...]  
  
//inside your widget tree...  
   Form(  
      key: _formKey,  
      child: Column(  
      children: [  
         TextFormField(  
             controller: tecUser,
             validator: (value) {  
               //your validation code: return null when value is right  
               //or a string if there's some error
             },  
             decoration: InputDecoration(hintText: "username".tr()),  
         ),  
         const SizedBox(height: 10),  
         TextFormField(  
             controller: tecPwd,
             validator: (value) {  
               //your validation code: return null when value is right  
               //or a string if there's some error
             },  
             obscureText: true,    
         ),   
         const SizedBox(height: 10),  
         OutlinedButton(child: const Icon(Icons.login), onPressed: () => _submit()),  

[...]   
  
void _submit() async {   
    if (_formKey.currentState!.validate()) {   
       //all validators returned null, so you can proceed with your logic
    } else {
       //this happens when at least one of the validators returned a string

       //by default, the error string returned by the validators will be displayed
       //near each field, so you won't have to worry about handling the error cases  and the else here won't even be necessary
    }
}

This is an excerpt from an actual login form.

EDIT: Ok, now I understand what you want to do. You have a group of fields that aren't mandatory, but they instead are mandatory if at least one of them has some value.
You need to assign a different TextEditingController to each of this fields: then, you need to assign a validator to each FormField that does something like this:

   //insert all the TextEditingController in a list  
   var tecList = <TextEditingController>[tec1, tec2...]

   //then, set a validator like this
   (value) {
      bool notMandatory = true;
      for (var tec in tecList){  
          notMandatory = notMandatory  && tec.text.isEmpty;  
      }  
      if (!notMandatory) return "Fill all fields";
      //some other validation here
   }
  • Related