Home > Enterprise >  how can i trigger formkey validate on initState - Flutter
how can i trigger formkey validate on initState - Flutter

Time:08-09

When the user comes to the profile edit page, I want to trigger the validation of the empty fields in the form and have them drawn in red. How can I do that?

I don't think I need to share any code blocks because I want to trigger formKey's validate method when the page is opened.

CodePudding user response:

Use AutovalidateMode.always

TextFormField(
 autovalidateMode: AutovalidateMode.always,
   ....  
 )

CodePudding user response:

Make StatefullWidget and do like here:

final bool _validate = false; // add this line above of build method


TextFormField(
    validator: (value) {
        if (value.isEmpty) {
            setState(() {
                _validate == false;
            });
        } else {
             setState(() {
                 _validate == true;
             });
        }
        return null;
    },
    decoration: InputDecoration(
        errorText: _validate ? null : 'Value Can\'t Be Empty', // this is your error
    ),
)
  • Related