Skip to content

Commit b4baf55

Browse files
committed
[cookbook/form/dynamic_form_modification] Fix sample code for dynamic generation of submitted forms (3rd section)
1 parent 594821b commit b4baf55

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

cookbook/form/dynamic_form_modification.rst

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,8 @@ The type would now look like::
473473
namespace Acme\DemoBundle\Form\Type;
474474

475475
// ...
476-
Acme\DemoBundle\Entity\Sport;
477-
Symfony\Component\Form\FormInterface;
476+
use Acme\DemoBundle\Entity\Sport;
477+
use Symfony\Component\Form\FormInterface;
478478

479479
class SportMeetupType extends AbstractType
480480
{
@@ -485,32 +485,28 @@ The type would now look like::
485485
;
486486

487487
$formModifier = function(FormInterface $form, Sport $sport) {
488-
$positions = $data->getSport()->getAvailablePositions();
488+
$positions = $sport->getAvailablePositions();
489489

490490
$form->add('position', 'entity', array('choices' => $positions));
491-
}
491+
};
492492

493493
$builder->addEventListener(
494494
FormEvents::PRE_SET_DATA,
495-
function(FormEvent $event) {
496-
$form = $event->getForm();
497-
495+
function(FormEvent $event) use ($formModifier) {
498496
// this would be your entity, i.e. SportMeetup
499497
$data = $event->getData();
500498

501-
$formModifier($event->getForm(), $sport);
499+
$formModifier($event->getForm(), $data->getSport());
502500
}
503501
);
504502

505503
$builder->get('sport')->addEventListener(
506504
FormEvents::POST_BIND,
507505
function(FormEvent $event) use ($formModifier) {
508506
// It's important here to fetch $event->getForm()->getData(), as
509-
// $event->getData() will get you the client data (this is, the ID)
507+
// $event->getData() will get you the client data (that is, the ID)
510508
$sport = $event->getForm()->getData();
511509

512-
$positions = $sport->getAvailablePositions();
513-
514510
// since we've added the listener to the child, we'll have to pass on
515511
// the parent to the callback functions!
516512
$formModifier($event->getForm()->getParent(), $sport);

0 commit comments

Comments
 (0)