I have a Recipe entity with a set of step forms assigned.
User can sort steps with drag and drop and some javascript. To preserve the new order, I added the step_number field to each step, which is then automatically populated with javascript.
To make sure the steps are displayed in the correct order, I use @ORM\OrderBy({"step_number" = "ASC"}) in my recipe entity.
The only problem: if the user submits the form and has some errors, the form is displayed again, but not in the order of the right step, because they are not retrieved from the database.
I tried to solve this by manually ordering the collection using eventListener as follows:
$builder->get('steps')->addEventListener(FormEvents::SUBMIT, function(FormEvent $event){ $steps = $event->getData(); $steps[1]->setStepnumber('8');
Here's what the dump looks like:
array(2) { [0]=> object(stdClass)#1212 (8) { ["__CLASS__"]=> string(29) "CoBo\RecipeBundle\Entity\Step" ["id"]=> int(244) ["recipe"]=> string(31) "CoBo\RecipeBundle\Entity\Recipe" ["step_number"]=> string(1) "2" ["description"]=> string(3) "test description 1" } [1]=> object(stdClass)#1220 (8) { ["__CLASS__"]=> string(29) "CoBo\RecipeBundle\Entity\Step" ["id"]=> int(245) ["recipe"]=> string(31) "CoBo\RecipeBundle\Entity\Recipe" ["step_number"]=> string(1) "8" ["description"]=> string(4) "test description 2" } }
but the modification of $steps[1]->setStepnumber('8'); does not affect the rendered form. step[1] still has the old number. I don't know what I'm doing wrong here. Another approach to sorting steps will also be useful.
source share