-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Have a way to Force the update of form data after submit when handling dynamic forms #57942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
FormEvents::SUBMIT should be the correct event for you. You'll get the normalized data and you are able to use From the UX side, it doesn't feel right. Let's say you have three series A, B and C and three countries X, Y and Z. Series A is forbidden in country X. Then, I select series A and B and only select country Y. After submitting the form, countries X and Y are selected and I don't know why. For me, the API solution doesn't feel like a hack. After the user selects series A, the API will disable and check country X so the user automatically knows that series A is always forbidden in country X. And unchecking series A gives the user all choices. What's the drawback? It has no impact on the backend logic. |
In this case, if you only selected Y, after submitting the form, only country Y would be selected since the form is just sending data. I dont see why would X be selected. This example is not the most suitable since I am talking of presetting the form's data which the user can change as he wishes, it would (should) have no impact on the submission itself. In my use case, if country X is forbidden for series A, the user can still choose series A and country X. Its not an absolute rule, just a suggesstion. It is a hack for quite a few reasons but mostly, it reduces maintainability considerably. For instance, I need to have an API url distinct from my current URL to retrieve my forbidden countries. If you have lots of this kind of use case, you need a way to transfer your URL to your form somehow so that the form knows where to get your new list. When it comes to the JS part, you also need to handle a few edge cases, for example, to handle my use case:
And i have to handle like 10 of these, this one being the simplest. All this just to update the 'data' of a ChoiceType |
Hopefully I understood your use cases. If you have multiple of "these" which work almost the same, you may create a custom form type like the RepeatedType. Your type has two Inside your form type you add two event listeners:
You need to write a template for your type which also includes some JS code. Pass Finally, your form type should have its own data class to allow automatic data mapping. In the outer form types, you need to convert (normalize/denormalize) your data to the new data class. That may seem to be a hack or will create duplicated code, but allows you to use multiple of "these" without writing multiple API endpoints, multiple JS code and multiple form event listeners. |
you got it just fine 😁 In my initial post i actually metionned the data mapping solution.
Actually i have quite a few different logics to implement for each of "these" use cases which makes it difficult to make a CustomType, and i did a POC of the denormalized/normalized data, which works fine, but is more complex, and i found it to be even less maintainable than the JS solution which is why for me its a hack. We should not have to do this kind of code just for updating the checked options of a select list based on some user's input. We should definitely do this in a cleaner way, like having a way to explicitly update the |
I fully agree with @khalid-s : In my opinion, alternative solutions (JS + API or normalization/denormalization) add unnecessary maintenance overhead and complicate development. That’s why I believe introducing an explicit option in form events to handle such cases would enhance Symfony’s flexibility while keeping it clear and controlled. This would be a valuable improvement for many developers. |
Thank you for this issue. |
no this has not been resolved and still waiting for a reply |
Uh oh!
There was an error while loading. Please reload this page.
Description
Currently when a form is submitted, we can bring in some changes like adding/removing a field but we cannot change the data of other fields.
While this makes sense and we should definitely keep, since when using the form events, generally when handling dynamic fields, we post the form with errors and symfony needs to be able to redisplay the form's previous state.
But sometimes we do need to be able to change the form's data when there is an error in the form. This should be used with caution.
There has been a few questions on this on StackOverflow which remained unanswered:
https://stackoverflow.com/questions/69617171/symfony-form-event-change-choice-field-options-based-on-other-choice-field/78845395#78845395
https://stackoverflow.com/questions/78836877/symfony-post-submit-update-data-of-underlying-object/78845393#78845393
Example
For example, let's say i have 2 fields which are of
EntityType
, the second on beingmultiple => true
like this:By default, no values are selected in the
series
, I need to preset some values in theforbiddenCountries
, I would add an EventListener for the PRESET_DATA on the form. For this part no problem.By in my app, if the
series
field is null, theforbiddenCountries
should default to some values else, it should have other values preset.So to achieve this, I would need to listen to the POST_SUBMIT event of the
series
field, then update the data of theforbiddenCountries
. And this last part is not possible with symfony.-- hacks
If i want to handle this in symfony, I would need to use unmapped fields and work with denormalized data which I would then have to re-normalize.
The other solution, which is the one i opted for, use javascript along with an API. On change on the
series
field, I would take the value and call my API to get a fresh list of data to preset in myforbiddenCountries
and update it using js.Both hack comes with huge drawbacks.
This is why it would be great to have a way to "force" (meaning the dev expressly understand the consequences of this specific action) the update of the underlying data
The text was updated successfully, but these errors were encountered: