Skip to content

Commit 1cedd87

Browse files
authored
Add Validation Auto Focus Parameter (#884)
* Added parameter to form allowing user to choose whether or not the form auto focuses on the first field that fails validation. * Update form_builder.dart
1 parent 6788a45 commit 1cedd87

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lib/src/form_builder.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class FormBuilder extends StatefulWidget {
5858
/// and their enabled state will be ignored.
5959
final bool enabled;
6060

61+
/// Whether the form should auto focus on the first field that fails validation.
62+
final bool validationAutoFocus;
63+
6164
/// Creates a container for form fields.
6265
///
6366
/// The [child] argument must not be null.
@@ -70,6 +73,7 @@ class FormBuilder extends StatefulWidget {
7073
this.initialValue = const <String, dynamic>{},
7174
this.skipDisabled = false,
7275
this.enabled = true,
76+
this.validationAutoFocus = true,
7377
}) : super(key: key);
7478

7579
static FormBuilderState? of(BuildContext context) =>
@@ -152,7 +156,7 @@ class FormBuilderState extends State<FormBuilder> {
152156

153157
bool validate() {
154158
final hasError = !_formKey.currentState!.validate();
155-
if (hasError) {
159+
if (hasError && widget.validationAutoFocus) {
156160
final wrongFields =
157161
fields.values.where((element) => element.hasError).toList();
158162
wrongFields.first.requestFocus();

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version: 6.1.0+1
44
homepage: https://github.com/danvick/flutter_form_builder
55

66
environment:
7-
sdk: ">=2.12.0 <3.0.0"
7+
sdk: '>=2.12.0 <3.0.0'
88

99
dependencies:
1010
flutter:

0 commit comments

Comments
 (0)