reidha / checkbox_formfield

Flutter package
https://pub.dev/packages/checkbox_formfield
BSD 3-Clause "New" or "Revised" License
3 stars 8 forks source link

The validator doesn't work. #9

Closed jtaxiexpress closed 2 years ago

jtaxiexpress commented 2 years ago

I tried to move the validator in every way I could.

However, it did not work with this package.

reidha commented 2 years ago

@jtaxiexpress Hi! Could you share your code?

jtaxiexpress commented 2 years ago

I can't see the validator in my app. The status of my checkbox changes, so I can check myself in.

Container(child: (() {
              if (userAge < 20) {
                return Padding(
                  padding: const EdgeInsets.only(top: 10.0),
                  child: Form(
                    key: UniqueKey(),
                    child: CheckboxListTileFormField(
                      onSaved: (bool? value) {
                        isCheckBoxChange.state = value!;
                      },
                      onChanged: (value) {
                        if (value) {
                          print(value);
                        } else {
                          print(value);
                        }
                      },
                      validator: (value) {
                        if (value!) {
                          return null;
                        } else {
                          return 'False';
                        }
                      },
                      activeColor: Colors.white,
                      checkColor: Colors.grey,
                      title: Text(
                        "agree",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                );
              } else {
                return Text("");
              }
            })())
SimonIT commented 2 years ago

The default autovalidateMode is AutovalidateMode.disabled. So no validation is done without calling formGlobalKey.currentState.validate(). You have to change the autovalidateMode if you want different behavior

reidha commented 2 years ago

@jtaxiexpress https://github.com/reidha/checkbox_formfield/blob/master/example/lib/main.dart I added autovalidateMode to the sample just for a reference.