Skip to content
Advertisement

Dynamic embedded form with OneToOne relationship

I’m trying to create a form for an article with an addon (tender).

The article is in one-to-one relationship with the addon and the addon can be disabled (null).

If the checkbox with an addon is checked, the addon needs to be validated, then submitted together with the article.

The issue I’m having is, I’m unable to set the addon to null before the submission, what produces the errors – the form still tries to submit an addon tied to the article, while its values can’t be empty.

How can I disable the addon when the checkbox is unchecked and still submit the article to the database, but without the addon?

I’ve tried using form events to modify the event’s data, but it turned out that tender is not part of data during PRE_SUBMIT. If I try to modify the data during SUBMIT, then I have to alter the logic in the methods of an entity, but it doesn’t feel right and also causes some issues that I don’t remember and couldn’t work around. I’ve also tried using ’empty_data’ => null, thinking it would do exactly what I’ve wanted, but it turned out it has no effect on my form at all.

Article’s entity:

JavaScript

Addon’s entity:

JavaScript

Article’s form:

JavaScript

@Edit I’ve used: Symfony Custom Field or Dynamic Form?

and added:

JavaScript

combined with the answer below and $content->setTender(new ContentsTenders()); in the controller, +reset method in model, it works.

It might not be a perfect solution, though.

Advertisement

Answer

You can use a validation group

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement