Setting validateAll in a FormGroupCubit while also using setAutovalidate(true) causes immediate validation of the form. The validation should not happen until any of the fields is changed. The below test should pass, but it does not.
test(
'validateAll does not cause immediate validation when used along setAutovalidate(true)',
() async {
final fieldA = TextFieldCubit<bool>(validator: (_) => false);
FormGroupCubit(validateAll: true)
..registerFields([fieldA])
..setAutovalidate(true);
await Future<void>.delayed(Duration.zero);
expect(fieldA.state.isValid, true);
});
per my understanding, the cause is as follows:
- in
registerFields a value is added to the _fieldController:
// inform that the fields have changed
_fieldsController.add(null);
setAutovalidate(true) is called synchronously after registerFields
- in the following event loop cycle,
_onFieldsStateChanged listener is triggered by the item added in 1). Because validateAll is set to true and we called setAutovalidate, validation is triggered.
The issue can be worked around by postponing setValidate(true) until the listener's callback is executed:
Future.microtask(() => setValidate(true));
Setting
validateAllin aFormGroupCubitwhile also usingsetAutovalidate(true)causes immediate validation of the form. The validation should not happen until any of the fields is changed. The below test should pass, but it does not.per my understanding, the cause is as follows:
registerFieldsa value is added to the_fieldController:setAutovalidate(true)is called synchronously afterregisterFields_onFieldsStateChangedlistener is triggered by the item added in 1). BecausevalidateAllis set totrueand we calledsetAutovalidate, validation is triggered.The issue can be worked around by postponing
setValidate(true)until the listener's callback is executed:Future.microtask(() => setValidate(true));