From b4baf5551feb7de5e47c94bdd37ea40e591f27a7 Mon Sep 17 00:00:00 2001 From: Philipp Rieber Date: Sat, 14 Sep 2013 21:12:42 +0200 Subject: [PATCH] [cookbook/form/dynamic_form_modification] Fix sample code for dynamic generation of submitted forms (3rd section) --- cookbook/form/dynamic_form_modification.rst | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/cookbook/form/dynamic_form_modification.rst b/cookbook/form/dynamic_form_modification.rst index 5542736e09a..ab68deecf80 100644 --- a/cookbook/form/dynamic_form_modification.rst +++ b/cookbook/form/dynamic_form_modification.rst @@ -473,8 +473,8 @@ The type would now look like:: namespace Acme\DemoBundle\Form\Type; // ... - Acme\DemoBundle\Entity\Sport; - Symfony\Component\Form\FormInterface; + use Acme\DemoBundle\Entity\Sport; + use Symfony\Component\Form\FormInterface; class SportMeetupType extends AbstractType { @@ -485,20 +485,18 @@ The type would now look like:: ; $formModifier = function(FormInterface $form, Sport $sport) { - $positions = $data->getSport()->getAvailablePositions(); + $positions = $sport->getAvailablePositions(); $form->add('position', 'entity', array('choices' => $positions)); - } + }; $builder->addEventListener( FormEvents::PRE_SET_DATA, - function(FormEvent $event) { - $form = $event->getForm(); - + function(FormEvent $event) use ($formModifier) { // this would be your entity, i.e. SportMeetup $data = $event->getData(); - $formModifier($event->getForm(), $sport); + $formModifier($event->getForm(), $data->getSport()); } ); @@ -506,11 +504,9 @@ The type would now look like:: FormEvents::POST_BIND, function(FormEvent $event) use ($formModifier) { // It's important here to fetch $event->getForm()->getData(), as - // $event->getData() will get you the client data (this is, the ID) + // $event->getData() will get you the client data (that is, the ID) $sport = $event->getForm()->getData(); - $positions = $sport->getAvailablePositions(); - // since we've added the listener to the child, we'll have to pass on // the parent to the callback functions! $formModifier($event->getForm()->getParent(), $sport);