[Form] Add is valid to FormState#48948
Conversation
|
Thanks for the contribution. It looks like the analyzer is not happy. Can you fix that please? |
Done. |
goderbauer
left a comment
There was a problem hiding this comment.
As mentioned in the underling issue (#48874 (comment)) I am not sure if adding this is a good idea.
|
Away for Chinese New Year now so I'm not with my workstation, will fix this PR after the holiday. Thanks for reviewing, Happy Holidays. |
Happy Holidays to you as well, and thank you for the contribution! I will mark this as work in progress so it won't be pinged for updates. :) |
|
I am still not convinced that this is the right way forward. It doesn't fit in with the existing form API. There's also the discussion in #48155, which should probably be resolved first before we add more API. |
justinmc
left a comment
There was a problem hiding this comment.
I'm going to side with supporting this change (with a few small changes below).
It seems to me that currently it's very awkward to check if a field is valid without causing the error on screen to update. Here is how I would do it, but please critique if there is a better way! https://dartpad.dartlang.org/34b602fa0a542a329dc5a5171ead9fc0
Besides requiring you to use a TextEditingController and a separate validation function, this isn't as robust as it could be. If I change the form field to use a different validation function instead of _validateEmail, I'll have to remember to update my code that checks if it's valid too. If you imagine this pattern on a very large form (@tjonganthony said he has 15 fields), then it's probably very messy.
I'm wary of adding complexity to a widget, but isValid is a very simple getter, and I don't see any problem with the pattern of passively checking if a field is valid. (Actually, it's validate() that I don't like, because it's an imperative method in a declarative framework.)
As I see it, I advocate for merging this and holding off on #48155. I'm open to discussion on all of this though!
This will allow FormState to check the validity of every field without updating the UI
Fix grammar. Co-Authored-By: Mohit Kanwal <creativepsyco@users.noreply.github.com>
3c914da to
935e9bf
Compare
|
@tjonganthony I was talking this over with @goderbauer and @HansMuller and we can't think of a common use case for having |
|
@justinmc technically I don't really mind since we can always hold the key to each form field and get the state. However I wonder what's the reasoning behind not adding it to Form? Personally I don't think it would cause harm to have this state. Maybe a bit of background for our project: This is the current behaviour of our native app before we migrate to flutter. Our UX wants user to at least edit the field before error is shown on that field, and keep the button disabled until the form is valid. |
I agree with this if you consider the use cases from user's point of view:
If you take away the last use case so that user cannot submit the form, the user no longer knows why the form cannot be submitted. The developer also no longer knows what the intent of the user is. Are they trying to submit? or are they still trying to fill out the form? That's where the harm is. If you put isValid() on the entire form without triggering errors, you encourage people to silently validate an entire form which should theoretically only be done upon user action. As you said, you can still do this but Flutter, as a framework, should not encourage it. |
+1 to this. I am ok with having a validator on the form field, but putting it on the entire From seems off. |
|
Alright, that all sounds fair. As I mentioned earlier, I don't have any issue with this behaviour being discouraged. Changed as per suggested :) |
goderbauer
left a comment
There was a problem hiding this comment.
/cc @justinmc for detailed review.
justinmc
left a comment
There was a problem hiding this comment.
LGTM, just a few optional nits. Thanks for all the discussion on these issues, I think we're really making our forms better 👍
Description
Added
isValidmethod that only checks whether everyFieldof aFormis valid.Related Issues
Fixes #48874
Tests
I added the following tests:
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]). This will ensure a smooth and quick review process.///).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.