From 5b678526a7fe759a9b595935f0425f4cd3d94106 Mon Sep 17 00:00:00 2001 From: Bart van den Burg Date: Sat, 24 Aug 2013 15:11:53 +0200 Subject: [PATCH 1/2] removed the $factory variable which is no longer needed in 2.2 --- cookbook/form/dynamic_form_modification.rst | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cookbook/form/dynamic_form_modification.rst b/cookbook/form/dynamic_form_modification.rst index 5b8d326d520..54f63f2d29f 100644 --- a/cookbook/form/dynamic_form_modification.rst +++ b/cookbook/form/dynamic_form_modification.rst @@ -270,11 +270,9 @@ and fill in the listener logic:: ); } - $factory = $builder->getFormFactory(); - $builder->addEventListener( FormEvents::PRE_SET_DATA, - function(FormEvent $event) use($user, $factory){ + function(FormEvent $event) use ($user) { $form = $event->getForm(); $formOptions = array( @@ -289,7 +287,7 @@ and fill in the listener logic:: // create the field, this is similar the $builder->add() // field name, field type, data, options - $form->add($factory->createNamed('friend', 'entity', null, $formOptions)); + $form->add('friend', 'entity', $formOptions); } ); } From 3364c8a73c9fb592f0a3a14fc88952b4a7c8aa73 Mon Sep 17 00:00:00 2001 From: Bart van den Burg Date: Sat, 24 Aug 2013 15:22:57 +0200 Subject: [PATCH 2/2] Fixed a mistake in the docs about form creation (you cannot simply call "->createForm()" in "any service that has access to the form factory") --- cookbook/form/dynamic_form_modification.rst | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cookbook/form/dynamic_form_modification.rst b/cookbook/form/dynamic_form_modification.rst index 54f63f2d29f..8cada9b6221 100644 --- a/cookbook/form/dynamic_form_modification.rst +++ b/cookbook/form/dynamic_form_modification.rst @@ -370,16 +370,22 @@ it with :ref:`dic-tags-form-type`. If you wish to create it from within a controller or any other service that has access to the form factory, you then use:: - class FriendMessageController extends Controller + use Symfony\Component\DependencyInjection\ContainerAware; + + class FriendMessageController extends ContainerAware { public function newAction(Request $request) { - $form = $this->createForm('acme_friend_message'); + $form = $this->get('form.factory')->create('acme_friend_message'); // ... } } +If you extend the ``Symfony\Bundle\FrameworkBundle\Controller\Controller`` class, you can simply call:: + + $form = $this->createForm('acme_friend_message'); + You can also easily embed the form type into another form:: // inside some other "form type" class