Skip to content

Commit

Permalink
Merge pull request #1342 from lreardon/patch-1
Browse files Browse the repository at this point in the history
Reintroduce ability to validate form field's value independent of input's error text
  • Loading branch information
deandreamatias authored Jan 5, 2024
2 parents 04660f1 + 6e55bbf commit ba065e5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/src/form_builder_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, 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 =>
Expand Down
40 changes: 40 additions & 0 deletions test/src/form_builder_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormBuilderFieldState>();
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<FormBuilderFieldState>();
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',
Expand Down

0 comments on commit ba065e5

Please sign in to comment.