Skip to content

feat: add asyncValidator support to FormBuilderField (#838) - #1514

Open
GhagSagar23 wants to merge 5 commits into
flutter-form-builder-ecosystem:mainfrom
GhagSagar23:feat/issue-838
Open

feat: add asyncValidator support to FormBuilderField (#838)#1514
GhagSagar23 wants to merge 5 commits into
flutter-form-builder-ecosystem:mainfrom
GhagSagar23:feat/issue-838

Conversation

@GhagSagar23

Copy link
Copy Markdown
Contributor

Summary

Adds asynchronous validation support to FormBuilderField, addressing #838.

New API

API Location Description
asyncValidator FormBuilderField Future<String?> Function(T?) — runs after sync validation passes
validateAsync() FormBuilderFieldState Runs sync → async validation for a single field
validateAsync() FormBuilderState Validates all fields (including async) in parallel
saveAndValidateAsync() FormBuilderState Saves then validates all fields asynchronously
isValidating FormBuilderFieldState true while an async validator is in-flight

Design

  • Opt-in only — async validation runs via validateAsync(), never auto-triggered on keystroke (avoids rebuild loops).
  • Stale result discarding — monotonic counter drops results from superseded validations.
  • Clean statedidChange() clears async errors; reset() cancels in-flight validations.

Usage

FormBuilderTextField(
  name: 'username',
  asyncValidator: (value) async {
    final exists = await api.checkUsername(value);
    return exists ? 'Username taken' : null;
  },
)

final isValid = await formKey.currentState!.validateAsync();

Tests

All 151 tests passed
00:05 +149: FormBuilderField - asyncValidator - Should validate asynchronously and update errorText
00:05 +150: FormBuilderField - asyncValidator - Should discard stale validations when value changes rapidly
00:05 +151: All tests passed!

Closes #838

…ilder-ecosystem#838)

Introduces an optional asyncValidator callback on FormBuilderField,
validateAsync() on both FormBuilderFieldState and FormBuilderState,
and saveAndValidateAsync() on FormBuilderState.

- asyncValidator: Future<String?> Function(T?) runs after sync validation passes
- Stale results are discarded via a request counter (_asyncValidationCount)
- isValidating getter exposes loading state for UI spinners
- didChange() and reset() clear async error state
- Forwarded through FormBuilderFieldDecoration and FormBuilderTextField

Closes flutter-form-builder-ecosystem#838
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.98990% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.45%. Comparing base (f8706f7) to head (ffbf57e).

Files with missing lines Patch % Lines
lib/src/form_builder_field.dart 98.78% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1514      +/-   ##
==========================================
+ Coverage   92.35%   93.45%   +1.10%     
==========================================
  Files          21       21              
  Lines         863      948      +85     
==========================================
+ Hits          797      886      +89     
+ Misses         66       62       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

    - Add test for `saveAndValidateAsync` and `autoScrollWhenFocusOnInvalid` in FormBuilder
    - Add tests for exceptions thrown inside `asyncValidator` in FormBuilderField
    - Add test for `validateAsync(clearCustomError: true)` clearing previous custom/async errors
    - Add test for auto-scroll trigger when synchronous validation fails before async validation runs
@github-actions github-actions Bot added the Stale label Aug 18, 2026
@deandreamatias

Copy link
Copy Markdown
Collaborator

Hi! Thanks for contribution

  • Please add some doc on README about this new feature.
  • How this interact with AutovalidateMode? I mean, the sync validator takes into account the AutovalidateMode set on FormBuilder and FormFieldBuilder

@deandreamatias deandreamatias added awaiting author response Waiting for author of issue to respond with more info and removed Stale labels Aug 19, 2026
@GhagSagar23

Copy link
Copy Markdown
Contributor Author

Hi! Thanks for contribution

  • Please add some doc on README about this new feature.
  • How this interact with AutovalidateMode? I mean, the sync validator takes into account the AutovalidateMode set on FormBuilder and FormFieldBuilder

Hey @deandreamatias, both the points have been addressed in the latest commit. Can you please re-review this PR?

@github-actions github-actions Bot added the agent-slop Detected AI agent slop label Aug 20, 2026
@github-actions

Copy link
Copy Markdown

This PR was flagged by our automated quality checks. If you're a genuine
contributor, please reply here and a maintainer will review your PR.

Common reasons for flagging:

  • New GitHub account
  • Unusually high number of repository forks in a 24-hour window

We appreciate your contribution and apologize if this is a false positive!

@GhagSagar23

Copy link
Copy Markdown
Contributor Author

This PR was flagged by our automated quality checks. If you're a genuine contributor, please reply here and a maintainer will review your PR.

Common reasons for flagging:

  • New GitHub account
  • Unusually high number of repository forks in a 24-hour window

We appreciate your contribution and apologize if this is a false positive!

Genuine contributor here — not agent-generated. I picked up #838 because I needed async validation in a production app and the issue has been open since 2021.

The implementation went through review with @deandreamatias already (added README docs and clarified the AutovalidateMode interaction per his feedback). Happy to answer any further questions.

@deandreamatias

Copy link
Copy Markdown
Collaborator

@GhagSagar23 could be share your production app? A link from Google Play or App Store could be enough

@GhagSagar23

Copy link
Copy Markdown
Contributor Author

@GhagSagar23 could be share your production app? A link from Google Play or App Store could be enough

Hi @deandreamatias, my app is not yet deployed on either of the stores. It still in development phase.

@deandreamatias

Copy link
Copy Markdown
Collaborator

So isn't a production app, yet, right?

@GhagSagar23

Copy link
Copy Markdown
Contributor Author

So isn't a production app, yet, right?

Not yet @deandreamatias

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-slop Detected AI agent slop awaiting author response Waiting for author of issue to respond with more info

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Asynchronous validator

2 participants