fix autovalidate in form widget to validate only when value changes#56132
fix autovalidate in form widget to validate only when value changes#56132ashank96 wants to merge 5 commits into
Conversation
|
This PR is easily the simplest (maybe even best) solution to that particular issue where autovalidate is not supposed to show error when screen is rendered for first time. The discussion at #48155 is part of a bigger issue and about having more control over validation and an entirely different way of handling it. I'm not fully sure if that is going to be merged anytime soon. In the meanwhile, this PR serves as the right fix for the initial render error text issue. |
|
@SpajicM @justinmc Hence, as @nateshmbhat seconds, this issue is quite common across all the firms/developers who are using forms in production, and needs an update on this as soon as possible. This PR can be a small but useful fix to the same. |
|
@ashank96 @nateshmbhat I totally agree, I only linked the other PR for informational purposes. |
|
@justinmc Can you please review the PR ? TIA. |
|
Sorry for the delay in taking a look at this and thanks for opening a PR. This is going to be tricky because it skirts the definition of a breaking change (see Flutter's breaking change policy). I have been trying to avoid changing this behavior directly because it seems to me that there could be apps that are relying on this behavior. I'm going to try to get a conclusion on the discussion of how to fix this long term (started in #48155). If that solution allows for changing this behavior then I can try to check if this change would break anyone's app and figure out a way forward. I'll try to post back with updates. |
|
If we're concerned about the breaking change, May be we could add an additional optional parameter (say validateOnFirstBuild) which defaults to true for backward compatibility. But honestly, I feel we need to fix this even if it's a breaking change because this behavior is not something 99% would want. Adding a flag like that again just for backward compatibility (which was in fact a bug) is like not taking care of the bug in some way. |
| // Only autovalidate if the widget is also enabled | ||
| if (widget.autovalidate && widget.enabled) | ||
| // Only autovalidate if the widget is also enabled and state value has changed at least once | ||
| if (widget.autovalidate && widget.enabled && _isStateDirty) |
There was a problem hiding this comment.
There is also a reset method, it will be called when the the form reset. In that case you might want to reset this state dirty to false
There was a problem hiding this comment.
yes, this PR tries to achieve the same thing as the other one you are referring to. It's just that this PR was raised pretty earlier than the other one.
@justinmc Regarding your concern, before the developer calls |
|
Another thing to think about is the form.autovalidate which is doing the same thing except it only validate after the second build. |
Description
The current
formwidget runs_validatefunction on every new build even when the form state is not changed, ifwidget.autovalidate == true, which as a result runs validations over all the fields in the form and you see errors corresponding each field due to empty field validation logic(if written by the developer), as soon as the user lands on to the screen.The PR modifies the autovalidate feature such that it functions only when the state value for a field is changed at least once.Related Issues
36154
48876
Tests
I added the following tests:
in
flutter_test.dartfile added test case under descriptionautovalidate functions only when state value changes at least onceChecklist
///).flutter analyze --flutter-repo) does not report any problems on my PR.Breaking Change
Did any tests fail when you ran them? Please read [Handling breaking changes].