diff --git a/lib/src/form_builder_field.dart b/lib/src/form_builder_field.dart index 03827a8c0..81bfea8c0 100644 --- a/lib/src/form_builder_field.dart +++ b/lib/src/form_builder_field.dart @@ -99,6 +99,9 @@ class FormBuilderFieldState, T> @override bool get isValid => super.isValid && errorText == null; + bool get valueIsValid => super.isValid; + bool get valueHasError => super.hasError; + bool get enabled => widget.enabled && (_formBuilderState?.enabled ?? true); bool get readOnly => !(_formBuilderState?.widget.skipDisabled ?? false); bool get _isAlwaysValidate => diff --git a/test/src/form_builder_field_test.dart b/test/src/form_builder_field_test.dart index 1af8a610b..24ce41ffc 100644 --- a/test/src/form_builder_field_test.dart +++ b/test/src/form_builder_field_test.dart @@ -125,6 +125,46 @@ void main() { }); }); + group('valueIsValid -', () { + testWidgets( + 'Should value is valid when validator passes, despite set custom error', + (tester) async { + final textFieldKey = GlobalKey(); + const textFieldName = 'text'; + const errorTextField = 'error text field'; + final testWidget = FormBuilderTextField( + name: textFieldName, + key: textFieldKey, + ); + await tester.pumpWidget(buildTestableFieldWidget(testWidget)); + + // Set custom error + textFieldKey.currentState?.invalidate(errorTextField); + await tester.pumpAndSettle(); + + expect(textFieldKey.currentState?.valueIsValid, isTrue); + }); + }); + + group('valueHasError -', () { + testWidgets('Should value is invalid when validator passes', + (tester) async { + final textFieldKey = GlobalKey(); + const textFieldName = 'text'; + const invalidValue = 'invalid'; + final testWidget = FormBuilderTextField( + name: textFieldName, + key: textFieldKey, + initialValue: invalidValue, + validator: (value) => (value == invalidValue) ? 'error' : null, + autovalidateMode: AutovalidateMode.always, + ); + await tester.pumpWidget(buildTestableFieldWidget(testWidget)); + + expect(textFieldKey.currentState?.valueHasError, isTrue); + }); + }); + group('autovalidateMode -', () { testWidgets( 'Should show error when init form and AutovalidateMode is always',