@@ -270,11 +270,9 @@ and fill in the listener logic::
270
270
);
271
271
}
272
272
273
- $factory = $builder->getFormFactory();
274
-
275
273
$builder->addEventListener(
276
274
FormEvents::PRE_SET_DATA,
277
- function(FormEvent $event) use($user, $factory) {
275
+ function(FormEvent $event) use ($user) {
278
276
$form = $event->getForm();
279
277
280
278
$formOptions = array(
@@ -289,7 +287,7 @@ and fill in the listener logic::
289
287
290
288
// create the field, this is similar the $builder->add()
291
289
// field name, field type, data, options
292
- $form->add($factory->createNamed( 'friend', 'entity', null, $formOptions) );
290
+ $form->add('friend', 'entity', $formOptions);
293
291
}
294
292
);
295
293
}
@@ -372,16 +370,22 @@ it with :ref:`dic-tags-form-type`.
372
370
If you wish to create it from within a controller or any other service that has
373
371
access to the form factory, you then use::
374
372
375
- class FriendMessageController extends Controller
373
+ use Symfony\Component\DependencyInjection\ContainerAware;
374
+
375
+ class FriendMessageController extends ContainerAware
376
376
{
377
377
public function newAction(Request $request)
378
378
{
379
- $form = $this->createForm ('acme_friend_message');
379
+ $form = $this->get('form.factory')->create ('acme_friend_message');
380
380
381
381
// ...
382
382
}
383
383
}
384
384
385
+ If you extend the ``Symfony\Bundle\FrameworkBundle\Controller\Controller `` class, you can simply call::
386
+
387
+ $form = $this->createForm('acme_friend_message');
388
+
385
389
You can also easily embed the form type into another form::
386
390
387
391
// inside some other "form type" class
0 commit comments