@@ -243,15 +243,6 @@ service into the form type so you can get the current user object::
243243 $this->security = $security;
244244 }
245245
246- .. note ::
247-
248- You might wonder, now that you have access to the ``User `` object, why not
249- just use it directly in ``buildForm() `` and omit the event listener? This is
250- because doing so in the ``buildForm() `` method would result in the whole
251- form type being modified and not just this one form instance. This may not
252- usually be a problem, but technically a single form type could be used on a
253- single request to create many forms or fields.
254-
255246Customizing the Form Type
256247~~~~~~~~~~~~~~~~~~~~~~~~~
257248
@@ -292,37 +283,41 @@ security helper to fill in the listener logic::
292283 );
293284 }
294285
295- $builder->addEventListener(
296- FormEvents::PRE_SET_DATA,
297- function (FormEvent $event) use ($user) {
298- $form = $event->getForm();
299-
300- $formOptions = array(
301- 'class' => User::class,
302- 'choice_label' => 'fullName',
303- 'query_builder' => function (EntityRepository $userRepository) use ($user) {
304- // build a custom query
305- // return $userRepository->createQueryBuilder('u')->addOrderBy('fullName', 'DESC');
306-
307- // or call a method on your repository that returns the query builder
308- // return $userRepository->createOrderByFullNameQueryBuilder();
309- },
310- );
311-
312- // create the field, this is similar the $builder->add()
313- // field name, field type, data, options
314- $form->add('friend', EntityType::class, $formOptions);
286+ $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($user) {
287+ if (null !== $event->getData()->getFriend()) {
288+ // we don't need to add the friend field because
289+ // the message will be addressed to a fixed friend
290+ return;
315291 }
316- );
292+
293+ $form = $event->getForm();
294+
295+ $formOptions = array(
296+ 'class' => User::class,
297+ 'choice_label' => 'fullName',
298+ 'query_builder' => function (UserRepository $userRepository) use ($user) {
299+ // call a method on your repository that returns the query builder
300+ // return $userRepository->createFriendsQueryBuilder($user);
301+ },
302+ );
303+
304+ // create the field, this is similar the $builder->add()
305+ // field name, field type, field options
306+ $form->add('friend', EntityType::class, $formOptions);
307+ });
317308 }
318309
319310 // ...
320311 }
321312
322313.. note ::
323314
324- The ``multiple `` and ``expanded `` form options will default to false
325- because the type of the friend field is ``EntityType::class ``.
315+ You might wonder, now that you have access to the ``User `` object, why not
316+ just use it directly in ``buildForm() `` and omit the event listener? This is
317+ because doing so in the ``buildForm() `` method would result in the whole
318+ form type being modified and not just this one form instance. This may not
319+ usually be a problem, but technically a single form type could be used on a
320+ single request to create many forms or fields.
326321
327322Using the Form
328323~~~~~~~~~~~~~~
0 commit comments