Skip to content

validateAll and autovalidate set to true trigger validation of fields which have not been modified #31

@gogolon

Description

@gogolon

My expectation is that validateAll only triggers validation on fields that have changed from their initial value. Currently, if setAutovalidate(true) is used, setting any field in the entire form causes all other fields to be validated. If their initial values don't pass the validation (which is very often the case), validation errors are emitted. I would expect the below test to pass, because fieldA has not changed.

    test(
        'validation triggered to validateAll only affects fields that have had setValue called on them',
        () async {
      final fieldA = TextFieldCubit<bool>(validator: (_) => false);
      final fieldB = TextFieldCubit<bool>(validator: (_) => false);
      final formCubit = FormGroupCubit(validateAll: true)
        ..registerFields([fieldA, fieldB]);
      await Future.microtask(
        () => formCubit.setAutovalidate(true),
      ); // This is required due to https://github.com/leancodepl/leancode_forms/issues/29
      await Future<void>.delayed(Duration.zero);
      fieldB.setValue('Any value');
      await Future<void>.delayed(Duration.zero);
      expect(fieldA.state.isValid, true);
    });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions