Skip to content

Commit 52fbc62

Browse files
Merge pull request #1234 from flutter-form-builder-ecosystem/feature-1014
Implement errors getter on form builder state
2 parents 81c3b97 + 16359a6 commit 52fbc62

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

lib/src/form_builder.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ class FormBuilderState extends State<FormBuilder> {
123123
/// Touched: The field is focused by user or by logic code.
124124
bool get isTouched => fields.values.any((field) => field.isTouched);
125125

126+
/// Get a map of errors
127+
Map<String, String> get errors => {
128+
for (var element
129+
in fields.entries.where((element) => element.value.hasError))
130+
element.key.toString(): element.value.errorText ?? ''
131+
};
132+
126133
/// Get initialValue.
127134
Map<String, dynamic> get initialValue => widget.initialValue;
128135

test/src/form_builder_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,67 @@ void main() {
398398
expect(find.text(errorTextField), findsNothing);
399399
});
400400
});
401+
402+
group('errors -', () {
403+
testWidgets('Should get errors when more than one fields are invalid',
404+
(tester) async {
405+
const textFieldName = 'text';
406+
const checkboxName = 'checkbox';
407+
const textFieldError = 'error text';
408+
const checkboxError = 'error checkbox';
409+
final testTextField = FormBuilderTextField(
410+
name: textFieldName,
411+
validator: (value) => textFieldError,
412+
);
413+
final testCheckbox = FormBuilderCheckbox(
414+
title: const Text('title'),
415+
name: checkboxName,
416+
validator: (value) => checkboxError,
417+
);
418+
await tester.pumpWidget(buildTestableFieldWidget(
419+
Column(children: [testTextField, testCheckbox]),
420+
autovalidateMode: AutovalidateMode.always,
421+
));
422+
423+
expect(
424+
formKey.currentState?.errors,
425+
equals({
426+
textFieldName: textFieldError,
427+
checkboxName: checkboxError,
428+
}),
429+
);
430+
});
431+
testWidgets('Should get errors when one field are invalid', (tester) async {
432+
const textFieldName = 'text';
433+
const textFieldError = 'error text';
434+
final testTextField = FormBuilderTextField(
435+
name: textFieldName,
436+
validator: (value) => textFieldError,
437+
);
438+
await tester.pumpWidget(buildTestableFieldWidget(
439+
testTextField,
440+
autovalidateMode: AutovalidateMode.always,
441+
));
442+
443+
expect(
444+
formKey.currentState?.errors,
445+
equals({textFieldName: textFieldError}),
446+
);
447+
});
448+
testWidgets('Should not get errors when all fields are valid',
449+
(tester) async {
450+
const textFieldName = 'text';
451+
final testTextField = FormBuilderTextField(
452+
name: textFieldName,
453+
);
454+
await tester.pumpWidget(buildTestableFieldWidget(
455+
testTextField,
456+
autovalidateMode: AutovalidateMode.always,
457+
));
458+
459+
expect(formKey.currentState?.errors, equals({}));
460+
});
461+
});
401462
}
402463

403464
// simple stateful widget that can hide and show its child with the intent of

0 commit comments

Comments
 (0)