Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
Description
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:
Example
For example, let's say i have 2 fields which are ofEntityType
, the second on beingmultiple => true
like this:
->add('series', EntityType::class, [ 'label' => 'Collection', 'class' => Series::class, 'choice_label' => 'name',])->add('forbiddenCountries', EntityType::class, [ 'class' => Country::class, 'multiple' => true])
By default, no values are selected in theseries
, 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 theseries
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 theseries
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 theseries
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