1
votes

Hi im new to flutter.

Im using TextFormField widget to validate the input if the textformfield is empty and wrapping it with GlobalKey Form widget.

Can i ask if its possible just only check atleast one field is not empty then its valid.

TextField A = empty & TextField B = not empty :: valid
TextField A = not empty & TextField B = empty :: valid
TextField A = empty & TextField B = empty :: not valid

This is the situation there are two textformfield A and textformfield B atleast one must not be empty so it could be A or B. But if both is empty then the user must fill one textfield. My objective is all my textformfield has a validation but its okay if atleast one is filled or not empty.

final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

                Form(
                key: _formKey,
                child: Column(children: [
                  TextFormField(validator: (value) {
                    if (value.isEmpty) {
                      return "Please Fill";
                    }
                  }),
                  TextFormField(validator: (value) {
                    if (value.isEmpty) {
                      return "Please Fill";
                    }
                  }),
                  RaisedButton(
                    child: Text("Submit"),
                    onPressed: () async {
                        if (_formKey.currentState.validate()) {
                          return;
                        }
                        _formKey.currentState.save();
                        //Some Codes
                      },
                  )
                ]),
              ),

I was planing to change it to TextField widget and use setState({}) to check if atleast 1 got filled but i dont want to use setState. Is there a way to solve my problem?. Thanks

1

1 Answers

2
votes

Try this:

final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

                Form(
                key: _formKey,
                child: Column(children: [
                  TextFormField(validator: (value) {
                    if (value.isEmpty) {
                      return "Please Fill";
                    }
                    return null;
                  }),
                  TextFormField(validator: (value) {
                    if (value.isEmpty) {
                      return "Please Fill";
                    }
                    return null;
                  }),
                  RaisedButton(
                    child: Text("Submit"),
                    onPressed: () async {
                        if (_formKey.currentState.validate()) {
                          // If the form is valid, display a Snackbar.
                            Scaffold.of(context).showSnackBar(SnackBar(content: Text('Processing Data')));
                        }
                        _formKey.currentState.save();
                        //Some Codes
                      },
                  )
                ]),
              ),