@@ -244,16 +244,6 @@ done in the constructor::
244
244
$this->tokenStorage = $tokenStorage;
245
245
}
246
246
247
- .. note ::
248
-
249
- You might wonder, now that you have access to the User (through the token
250
- storage), why not just use it directly in ``buildForm() `` and omit the
251
- event listener? This is because doing so in the ``buildForm() `` method
252
- would result in the whole form type being modified and not just this
253
- one form instance. This may not usually be a problem, but technically
254
- a single form type could be used on a single request to create many forms
255
- or fields.
256
-
257
247
Customizing the Form Type
258
248
~~~~~~~~~~~~~~~~~~~~~~~~~
259
249
@@ -294,38 +284,41 @@ and fill in the listener logic::
294
284
);
295
285
}
296
286
297
- $builder->addEventListener(
298
- FormEvents::PRE_SET_DATA,
299
- function (FormEvent $event) use ($user) {
300
- $form = $event->getForm();
301
-
302
- $formOptions = array(
303
- 'class' => User::class,
304
- 'choice_label' => 'fullName',
305
- 'query_builder' => function (EntityRepository $er) use ($user) {
306
- // build a custom query
307
- // return $er->createQueryBuilder('u')->addOrderBy('fullName', 'DESC');
308
-
309
- // or call a method on your repository that returns the query builder
310
- // the $er is an instance of your UserRepository
311
- // return $er->createOrderByFullNameQueryBuilder();
312
- },
313
- );
314
-
315
- // create the field, this is similar the $builder->add()
316
- // field name, field type, data, options
317
- $form->add('friend', EntityType::class, $formOptions);
287
+ $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($user) {
288
+ if (null !== $event->getData()->getFriend()) {
289
+ // we don't need to add the friend field because
290
+ // the message will be addressed to a fixed friend
291
+ return;
318
292
}
319
- );
293
+
294
+ $form = $event->getForm();
295
+
296
+ $formOptions = array(
297
+ 'class' => User::class,
298
+ 'choice_label' => 'fullName',
299
+ 'query_builder' => function (UserRepository $userRepository) use ($user) {
300
+ // call a method on your repository that returns the query builder
301
+ // return $userRepository->createFriendsQueryBuilder($user);
302
+ },
303
+ );
304
+
305
+ // create the field, this is similar the $builder->add()
306
+ // field name, field type, field options
307
+ $form->add('friend', EntityType::class, $formOptions);
308
+ });
320
309
}
321
310
322
311
// ...
323
312
}
324
313
325
314
.. note ::
326
315
327
- The ``multiple `` and ``expanded `` form options will default to false
328
- because the type of the friend field is ``EntityType::class ``.
316
+ You might wonder, now that you have access to the ``User `` object, why not
317
+ just use it directly in ``buildForm() `` and omit the event listener? This is
318
+ because doing so in the ``buildForm() `` method would result in the whole
319
+ form type being modified and not just this one form instance. This may not
320
+ usually be a problem, but technically a single form type could be used on a
321
+ single request to create many forms or fields.
329
322
330
323
Using the Form
331
324
~~~~~~~~~~~~~~
0 commit comments