joanpablo / reactive_forms

This is a model-driven approach to handling form inputs and validations, heavily inspired in Angular's Reactive Forms
MIT License
471 stars 90 forks source link

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

Open ManuelRauber opened 11 months ago

ManuelRauber commented 11 months ago

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:

https://github.com/joanpablo/reactive_forms/blob/94c928df929f3618d322c894e3d9aad5423ea1ac/lib/src/models/form_builder.dart#L223-L246

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.