0

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.

  • Does this answer your question? [TextFormField validator not working - The method 'validate' was called on null](https://stackoverflow.com/questions/54925779/textformfield-validator-not-working-the-method-validate-was-called-on-null) – eamirho3ein Dec 13 '22 at 12:54

2 Answers2

0

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")                         
  }
Maximilian
  • 201
  • 1
  • 7
0

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
   }
il_boga
  • 1,305
  • 1
  • 13
  • 21
  • On validator:(value){}, how would you make the function to validate if one TextFormField field is NOT empty and the other TextFormField IS empty then it would not let you go through? – Eder Savillon Dec 13 '22 at 13:23
  • Each validator cares only for its `FormField`: the function that invokes all of them is `_formKey.currentState!.validate()`. This function calls all the `validator` of each `FormField` inside the `Form` widget assigned to the `FormKey`: if only one of them does not return `null`, the whole form is considered not valid and `validate()` returns `false` – il_boga Dec 13 '22 at 14:24
  • No, wait, I misunderstood: see my edit – il_boga Dec 13 '22 at 15:05
  • 1
    Yes, using this did the trick. This is the right answer, thank you so much! – Eder Savillon Dec 14 '22 at 14:39
  • 1
    Sorry, forgot to mark it. Thank you again! – Eder Savillon Dec 15 '22 at 13:01