Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReactiveDatePicker Validators not working when using fb syntax without FormControl() #428

Open
ManuelRauber opened this issue Dec 7, 2023 · 0 comments

Comments

@ManuelRauber
Copy link

Hi,

I've been trying out reactive_forms and I possibly stumbled upon a little issue: I want to add a validator to a DateTime field.

Using fb and FormControl I created the form this way:

fb.group({
            'dateError': FormControl<DateTime>(
              value: DateTime(2022),
              validators: [
                Validators.delegate(
                  (final control) => control.isNotNull && DateTime.now().isBefore(control.value)
                      ? null
                      : {'invalid-date': true},
                ),
              ],
            ),
          }),

This works as expected and my form goes into invalid state when I select a date before the current date.

However, if I create the form this way:

fb.group({
            'dateError': [
              DateTime(2022),
              Validators.delegate(
                (final control) => control.isNotNull && control.value > DateTime.now()
                    ? null
                    : {'invalid date': true},
              ),
            ],
          }),

It's not working anymore, the validator does not get executed.

I did a quick debug session and I possibly found a place:

FormControl<dynamic> _control(
dynamic value, List<Validator<dynamic>> validators) {
if (value is AbstractControl) {
throw FormBuilderInvalidInitializationException(
'Default value of control must not be an AbstractControl.');
}
if (value is String) {
return FormControl<String>(value: value, validators: validators);
} else if (value is int) {
return FormControl<int>(value: value, validators: validators);
} else if (value is bool) {
return FormControl<bool>(value: value, validators: validators);
} else if (value is double) {
return FormControl<double>(value: value, validators: validators);
} else if (value is DateTime) {
return FormControl<DateTime>(value: value);
} else if (value is TimeOfDay) {
return FormControl<TimeOfDay>(value: value);
}
return FormControl<dynamic>(value: value, validators: validators);
}
}

As you can see, the validators are missing for DateTime and TimeOfDay.

I understand that with the callbacks ReactiveDatePicker supports to only let the user select valid dates, there are use cases where the control could be invalid.
My use case for example is, that I load old data for migration and it could be, that a date is not in the required range anymore, so the user must select a new one.

It would be nice, if the alternative syntax could respect the validator settings as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant