V
V
Vladimir Leitenberger2015-01-21 19:11:09
symfony
Vladimir Leitenberger, 2015-01-21 19:11:09

Symfony2: How to create combi form from multiple entities?

Good afternoon. The task was to make a form from an entity. It seems nothing so complicated, but as it turned out, in some cases the number of entities from which it is necessary to make ONE form is more than 1 ... and then I got stuck.
so:

namespace TimeManagerBundle\Entity;

class Timepatch
{
  //PROPERTIES

    private $timepatchId;

    private $timepatchStart;

    private $timepatchEnd;

    private $note;

  // METHODS
  
    public function getTimepatchId() { /* ... */ }

    public function setTimepatchStart($timepatchStart) { /* ... */ }

    public function getTimepatchStart() { /* ... */ }

    public function setTimepatchEnd($timepatchEnd) { /* ... */ }

    public function getTimepatchEnd() { /* ... */ }

    public function setNote($note) { /* ... */ }

    public function getNote() { /* ... */ }

}

So here's how to make a form, with validation and other things, if there are two such entities, but the form should be one, that is, one tag, one CSRF token and one submit button. Is there any point in doing this at all, because I also don’t really know how to process this request ... PS If suddenly the description is not clear, then here are the pictures: So. Suppose some method from the repository returns me an array with the found Entities (Timepatch), which can contain either one or several entries: this array with the found entries must be presented in the form of ONE form in order for the user to correct the data (of one or more Entities [Timepatch]) and !at once! sent everything for processing / validation, etc. That is, the desired form can look either like this:<form>

e66c93966cb447e59393715e5340f822.PNG9062f781dafa4f5497d37872e7584a82.PNG
22762f0512a84caeb8d9a11d5b3a911f.PNG
or like this: 22a5598a8a504dddab02fdef2c63a72a.PNG
in the screenshot, the form is naturally not real in a simple copy / paste. The task is to create such a form

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-01-21
Protko @Fesor

Understood nothing.
Just to be clear, in the context of Symfony/forms it doesn't matter if you insert another form or an input into the form. The nesting can be arbitrarily large. If you just need to display two forms for two entities, make a third form and add these two to it.
In any case, you must have some kind of object for storing data, you can make a DTO with two fields in which your entities lie, then request processing will come down to creating a DTO object and binding the request using symphony.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question