Skip to content

Feat: Add patchError function to FormBuilder #1483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 57 additions & 10 deletions lib/src/form_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,10 @@
_focusOnInvalid = focusOnInvalid;
final hasError = !_formKey.currentState!.validate();
if (hasError) {
final wrongFields =
fields.values.where((element) => element.hasError).toList();
if (wrongFields.isNotEmpty) {
if (focusOnInvalid) {
wrongFields.first.focus();
}
if (autoScrollWhenFocusOnInvalid) {
wrongFields.first.ensureScrollableVisibility();
}
}
scrollToFirstInvalidField(
focusOnInvalid: focusOnInvalid,
autoScrollWhenFocusOnInvalid: autoScrollWhenFocusOnInvalid,
);
}
return !hasError;
}
Expand Down Expand Up @@ -349,6 +343,59 @@
});
}

/// Update fields errors of form.
/// Useful when need update errors at once, after got errors from server.
///
/// Focus to first invalid field when has field invalid, if [focusOnInvalid] is `true`.
/// By default `true`
///
/// Auto scroll to first invalid field focused if [autoScrollWhenFocusOnInvalid] is `true`.
/// By default `false`.
///
/// Note: If a invalid field is from type **TextField** and will focused,
/// the form will auto scroll to show this invalid field.
/// In this case, the automatic scroll happens because is a behavior inside the framework,
/// not because [autoScrollWhenFocusOnInvalid] is `true`.
void patchError(

Check warning on line 359 in lib/src/form_builder.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/form_builder.dart#L359

Added line #L359 was not covered by tests
Map<String, String> errors, {
bool focusOnInvalid = true,
bool autoScrollWhenFocusOnInvalid = false,
}) {
errors.forEach((key, value) {
_fields[key]?.invalidate(value);

Check warning on line 365 in lib/src/form_builder.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/form_builder.dart#L364-L365

Added lines #L364 - L365 were not covered by tests
});
scrollToFirstInvalidField(

Check warning on line 367 in lib/src/form_builder.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/form_builder.dart#L367

Added line #L367 was not covered by tests
focusOnInvalid: focusOnInvalid,
autoScrollWhenFocusOnInvalid: autoScrollWhenFocusOnInvalid,
);
}

/// Focus to first invalid field when has field invalid, if [focusOnInvalid] is `true`.
/// By default `true`
///
/// Auto scroll to first invalid field focused if [autoScrollWhenFocusOnInvalid] is `true`.
/// By default `false`.
///
/// Note: If a invalid field is from type **TextField** and will focused,
/// the form will auto scroll to show this invalid field.
/// In this case, the automatic scroll happens because is a behavior inside the framework,
/// not because [autoScrollWhenFocusOnInvalid] is `true`.
void scrollToFirstInvalidField({
bool focusOnInvalid = true,
bool autoScrollWhenFocusOnInvalid = false,
}) {
final wrongFields =
fields.values.where((element) => element.hasError).toList();
if (wrongFields.isNotEmpty) {
if (focusOnInvalid) {
wrongFields.first.focus();
}
if (autoScrollWhenFocusOnInvalid) {
wrongFields.first.ensureScrollableVisibility();

Check warning on line 394 in lib/src/form_builder.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/form_builder.dart#L394

Added line #L394 was not covered by tests
}
}
}

@override
void initState() {
// Verify if need auto validate form
Expand Down
Loading