Add autovalidateMode support (Fix #232)#233
Open
mem-5514-tahara wants to merge 1 commit into
Open
Conversation
Expose Flutter's AutovalidateMode on Pinput so callers can opt into onUserInteraction validation without waiting for full PIN entry. - Add autovalidateMode field (default: AutovalidateMode.disabled) to both Pinput() and Pinput.builder() constructors - Pass it through to _PinputFormField instead of hardcoding disabled - Defer setState in _validator() via addPostFrameCallback when called during FormField's build phase to avoid setState-during-build violation - Add 6 test cases covering disabled, onUserInteraction, error display, error clearing, default backward-compat, and Pinput.builder Closes Tkko#232
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hello @Tkko, thank you for your continuous work on pinput.
I've submitted this PR to fix #232 and introduce the requested autovalidateMode support.
Fix #232
Problem
Pinput's internal
_PinputFormFieldsubclasses Flutter'sFormFieldbut hardcodedautovalidateMode: AutovalidateMode.disabled, making it impossible for callers toopt into keystroke-level validation.
As a result,
validatoronly fired when all digits were entered (_maybeValidateForm)or on explicit form submission. Partial-input errors were silently swallowed.
Root cause & fix
Data flow with
onUserInteraction_PinputFormField— forward instead of hardcodesetState-during-build hazardWhen
AutovalidateMode.onUserInteractionis active,FormField.build()calls_validate()→_validator()during the build phase. Since_validator()callssetState, this triggers a setState-during-build violation.Fix: skip the user validator call entirely during
SchedulerPhase.persistentCallbacksand defer the full validation to the next frame. A
res != _validatorErrorTextguardprevents infinite
postFrameCallback → setState → rebuild → postFrameCallbackloops.Changed files
lib/src/pinput.dartautovalidateModefield + parameter to both constructors +debugFillPropertiesentry; addscheduler.dartimportlib/src/widgets/widgets.dartautovalidateModewith a forwarded named parameterlib/src/pinput_state.dartautovalidateModeto_PinputFormField; update_validator()with build-phase defer logictest/pinput_test.dartUsage
Tests
Six new cases in
group('autovalidateMode should work properly', ...):disabled— validator not called while typingonUserInteraction— validator called per keystroke_validatorErrorText+showErrorStateUI flowautovalidateModepreserves legacy behaviordisabledPinput.builderalso respects the parameterBackward compatibility
AutovalidateMode.disabled— existing code requires no changesPinputAutovalidateModeandpinputAutovalidateModeare untouchedDemo
Simulator.Screen.Recording.-.iPhone.16e.-.2026-06-05.at.17.55.39.mov
Verification code (before/after comparison page)