Add submit implementation to Form widget#107530
Conversation
submit implementation to Form widget
Co-authored-by: Pedro Massango <pedromassango.developer@gmail.com>
Co-authored-by: Pedro Massango <pedromassango.developer@gmail.com>
Co-authored-by: Pedro Massango <pedromassango.developer@gmail.com>
Co-authored-by: Pedro Massango <pedromassango.developer@gmail.com>
…aar/flutter into add_submit_to_form
|
@pedromassango failed test seems unrelated to this PR |
LongCatIsLooong
left a comment
There was a problem hiding this comment.
Following your example in the issue (#107528), it doesn't feel it's that difficult to aggregate a bunch of TextFormFields? Developers would have to create a TextEditingController for each of them anyways so you could do
{
'name': nameController.text,
'email': emailController.text,
}
in the submit method?
here is a real world example that (in my opinion) hows how much better this approach scales and improves reusability. https://gist.github.com/casvanluijtelaar/a6db35fb984cb41b1c99b45d7bf570bd |
justinmc
left a comment
There was a problem hiding this comment.
@casvanluijtelaar I've modified your gist to show what you'd have to do to achieve (mostly) the same thing without this PR: https://gist.github.com/justinmc/1ed6f6a2faa3bf5dc0a6ce83119afeeb
Does that cover your use case or am I missing anything? From that it looks like the hardest part is just getting access to all of the FormFields that belong to the Form. In my example I had to keep track of all of their keys myself.
What if we just made that part easier by exposing all of the FormFieldStates or their keys on FormState? I kind of wonder why that wasn't done originally when Form was written...
There was a problem hiding this comment.
Can you undo these formatting changes? I think it should be the way it was if I'm not mistaken. Here and elsewhere.
There was a problem hiding this comment.
@casvanluijtelaar I've modified your gist to show what you'd have to do to achieve (mostly) the same thing without this PR: https://gist.github.com/justinmc/1ed6f6a2faa3bf5dc0a6ce83119afeeb
Does that cover your use case or am I missing anything? From that it looks like the hardest part is just getting access to all of the FormFields that belong to the Form. In my example I had to keep track of all of their keys myself.
What if we just made that part easier by exposing all of the FormFieldStates or their keys on FormState? I kind of wonder why that wasn't done originally when Form was written...
more or less, yes. this PR tries to take advantage of the formSates ability to interact with all child Form Fields. without having to to the boilerplate of keeping track of them yourself. My original idea was to just do this as a method extension, but since the _fields aren't exposed I had to clone the form implementation and add it myself. hence the PR ;)
There was a problem hiding this comment.
and as said, your example would scale a lot harder with a more complex UI.
51d01ca to
a702262
Compare
|
failing tests seem unrelated to this change |
|
Could you try syncing the branch to |
|
In case there're lazily built |
Co-authored-by: Pedro Massango <pedromassango.developer@gmail.com>
interesting case, a formfield that hasn't been built so hasn't been interacted with doesn't matter, they are skipped anyway. when a formfield has some state it will be cached in the form. So we can still get that data. Any issue cases I am missing? |
|
Could you fix the error: |
|
After looking at https://gist.github.com/casvanluijtelaar/a6db35fb984cb41b1c99b45d7bf570bd .I think the API seems to be a bit limited to be used for JSON serialization:
Also FileField(
submissionKey: 'file',
hintText: 'important_file.pdf',
initialValue: post?.file,
validator: FeedFormValidators.fileValidator,
),doesn't seem to be that much simpler than this: final fileFieldController = ....;
FileField(
controller: fieldFieldController,
hintText: 'important_file.pdf',
initialValue: post?.file,
validator: FeedFormValidators.fileValidator,
),, but with the controller approach you could do this: |
|
good points yes, and it would be difficult to implement these ideas with this approach, maybe we should look at exposing the |
|
This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold. For more guidance, visit Writing a golden file test for Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
|
@casvanluijtelaar Checking in on your plans for this PR. Anything we can help with? |
The work in this PR is good to go. I'd like to see it merged :). But if it's decided this isn't a good match I'd like to see |
|
I like what I see in this PR cause I was trying to achieve the same, instead of keeping myself references to all TextEditingController and even more complex form with files and dropbox where it start to be boilerplate and repetitive if you have multiple forms. Having a submit method would help with that. Or as suggested at least expose |
|
For reasons stated in #107530 (comment), I think using |
Upon reading and taking the time to consider the above points, I'm left wondering if the solution that this PR proposes is in fact suboptimal.
As for the use of controllers, they certainly work beautifully for many scenarios, but not all. Some FormFields are not able to use them. For example, DropdownButtonFormField doesn't have a controller since controllers are not part of the FormField api. Additionally, controllers tend to introduce a lot of boilerplate code which can feel disruptive to the developer experience when used only for the sake of accessing the value of a form field. Truly, it would be a poor argument to state that the solution proposed by this PR is good simply because it follows the same design, for retrieving form field values, as other popular Flutter form libraries on pub.dev. However, the fact that these libraries have a fair amount of developers showing support for them does highlight a desire path created by the Flutter community for an improved form building experience, and the solution this PR proposes would indeed improve the form interface by making it significantly easier to retrieve form values. As a result, I think introducing a solution like this to the Flutter SDK is worth reconsidering so long as we can refactor it to address the concerns pointed out by @LongCatIsLooong. |
|
Hey @LongCatIsLooong 👋, I hope all is well! I'm just checking in to see if you were able to take a look at the comment I made. I'm hoping to see if we still can work to address the concerns you raised with this PR. |
|
@jaumard, it looks like @LongCatIsLooong might be assisting with different issues on the Flutter GitHub at the moment. Would you be willing to consider reopening this issue after reading my above comments? I'd like to see if we could still address the concerns, in this PR, that @LongCatIsLooong brought up. |
|
Sorry for the late reply. Exposing |
|
Upon first glance at the |
This pull request adds the
submitmethod toFormStateallowing you to get all the childsFormFieldStatevalues as a Map<String, dynamic> through eitherForm.of(context).submit()orformKey.currentState.submit().to achieve this a
submissionKeyis added to theFormFieldbase class.fixes #107528
Pre-launch Checklist
///).