Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Have a way to Force the update of form data after submit when handling dynamic forms #57942

Open
@khalid-s

Description

@khalid-s

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 being multiple => 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 the series, I need to preset some values in the forbiddenCountries, 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, the forbiddenCountries 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 the forbiddenCountries. 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 my forbiddenCountries 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions