Support initializing form fields outside of the build method #462
Labels
awaiting author response
Waiting for author of issue to respond with more info
enhancement
New feature or request
Stale
Often times, we have collections of related forms where at least one form is not immediately rendered onscreen and therefore not registered to the form key. The most common of these situations is when we have a multi-paged or tabbed form. In such cases, it's difficult, if not impossible at times to validate the collection of forms or even a single form rendered on screen (if it depends on some other form on another page or tab). This isn't a problem if a user is guaranteed to walk through the forms sequentially but very often we find the use case where the user want's to start on a specific page and then leave immediately after editing a particular topic, leading to validation errors.
Lets walk through an example:
Let's consider the scenario where we have some sort of university housing form. Users can go to the an Edit Profile page to edit their information which is automatically saved on exit but only after a successful form validation. The user's profile data is spread across two tabs, each containing a form with it's own form key, for their personal and housing data respectively.
Let's now say that the housing form has a form field that actually depends on values in one or more fields on the personal data form to validate. If the personal profile tab is always first to be rendered when the user enters the Edit Profile page, (because it is attached to the first tab), then there is no problem to validate on exit. Both forms must have been rendered and field values registered to their form keys after navigating over to the housing data tab from the initial personal data tab.
However, what happens when some action from outside the Edit Profile page navigates us directly to the housing data tab? The form will render without issue and the user, thinking all is well, edits a few fields on the housing tab and attempts to exit. Now we have a problem.The action errors out because our global key for the personal data form, referenced in our validation call in that housing data form field, is null. We never navigated to it.
This is just one example but similar situations present themselves in other multipage forms, very common in profile or carousel-like user onboarding forms.
I don't know what the solution should be but I think this is a common enough scenario inducing sufficient pain to start the conversation on how to address it in a future release. Most likely, this would imply re-assessing the modeling strategy we're currently using to store the form state. (Possibly related to #104) It would be great if there were some way to build the form model outside of the build() method, in initState() for example. I can work around this by writing my own form model classes that do the validation but I don't think that should be necessary because it makes me re-implement form validation on my own, rather than just using the one already built into this package.
The text was updated successfully, but these errors were encountered: