@@ -212,7 +212,6 @@ Using an event listener, your form might look like this::
212
212
use Symfony\Component\Form\FormBuilderInterface;
213
213
use Symfony\Component\Form\FormEvents;
214
214
use Symfony\Component\Form\FormEvent;
215
- use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
216
215
use Symfony\Component\Form\Extension\Core\Type\TextType;
217
216
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
218
217
@@ -231,33 +230,31 @@ Using an event listener, your form might look like this::
231
230
}
232
231
233
232
The problem is now to get the current user and create a choice field that
234
- contains only this user's friends.
233
+ contains only this user's friends. This can be done injecting the ``Security ``
234
+ service into the form type so you can get the current user object::
235
235
236
- Luckily it is pretty easy to inject a service inside of the form. This can be
237
- done in the constructor::
236
+ use Symfony\Component\Security\Core\Security;
238
237
239
- private $tokenStorage ;
238
+ private $security ;
240
239
241
- public function __construct(TokenStorageInterface $tokenStorage )
240
+ public function __construct(Security $security )
242
241
{
243
- $this->tokenStorage = $tokenStorage ;
242
+ $this->security = $security ;
244
243
}
245
244
246
245
.. note ::
247
246
248
- You might wonder, now that you have access to the User (through the token
249
- storage), why not just use it directly in ``buildForm() `` and omit the
250
- event listener? This is because doing so in the ``buildForm() `` method
251
- would result in the whole form type being modified and not just this
252
- one form instance. This may not usually be a problem, but technically
253
- a single form type could be used on a single request to create many forms
254
- or fields.
247
+ You might wonder, now that you have access to the ``User `` object, why not
248
+ just use it directly in ``buildForm() `` and omit the event listener? This is
249
+ because doing so in the ``buildForm() `` method would result in the whole
250
+ form type being modified and not just this one form instance. This may not
251
+ usually be a problem, but technically a single form type could be used on a
252
+ single request to create many forms or fields.
255
253
256
254
Customizing the Form Type
257
255
~~~~~~~~~~~~~~~~~~~~~~~~~
258
256
259
- Now that you have all the basics in place you can take advantage of the ``TokenStorageInterface ``
260
- and fill in the listener logic::
257
+ Now that you have all the basics in place you can complete the listener logic::
261
258
262
259
// src/Form/Type/FriendMessageFormType.php
263
260
@@ -266,16 +263,16 @@ and fill in the listener logic::
266
263
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
267
264
use Symfony\Component\Form\Extension\Core\Type\TextType;
268
265
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
269
- use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ;
266
+ use Symfony\Component\Security\Core\Security ;
270
267
// ...
271
268
272
269
class FriendMessageFormType extends AbstractType
273
270
{
274
- private $tokenStorage ;
271
+ private $security ;
275
272
276
- public function __construct(TokenStorageInterface $tokenStorage )
273
+ public function __construct(Security $security )
277
274
{
278
- $this->tokenStorage = $tokenStorage ;
275
+ $this->security = $security ;
279
276
}
280
277
281
278
public function buildForm(FormBuilderInterface $builder, array $options)
@@ -286,7 +283,7 @@ and fill in the listener logic::
286
283
;
287
284
288
285
// grab the user, do a quick sanity check that one exists
289
- $user = $this->tokenStorage->getToken() ->getUser();
286
+ $user = $this->security ->getUser();
290
287
if (!$user) {
291
288
throw new \LogicException(
292
289
'The FriendMessageFormType cannot be used without an authenticated user!'
@@ -301,13 +298,12 @@ and fill in the listener logic::
301
298
$formOptions = array(
302
299
'class' => User::class,
303
300
'choice_label' => 'fullName',
304
- 'query_builder' => function (EntityRepository $er ) use ($user) {
301
+ 'query_builder' => function (EntityRepository $userRepository ) use ($user) {
305
302
// build a custom query
306
- // return $er ->createQueryBuilder('u')->addOrderBy('fullName', 'DESC');
303
+ // return $userRepository ->createQueryBuilder('u')->addOrderBy('fullName', 'DESC');
307
304
308
305
// or call a method on your repository that returns the query builder
309
- // the $er is an instance of your UserRepository
310
- // return $er->createOrderByFullNameQueryBuilder();
306
+ // return $userRepository->createOrderByFullNameQueryBuilder();
311
307
},
312
308
);
313
309
@@ -331,11 +327,8 @@ Using the Form
331
327
332
328
If you're using :ref: `autowire <services-autowire >` and
333
329
:ref: `autoconfigure <services-autoconfigure >`, your form is ready to be used!
334
-
335
- .. tip ::
336
-
337
- If you're not using autowire and autoconfigure, see :doc: `/form/form_dependencies `
338
- for how to register your form type as a service.
330
+ Otherwise, see :doc: `/form/form_dependencies ` to learn how to register your form
331
+ type as a service.
339
332
340
333
In a controller, create the form like normal::
341
334
<
10000
div class="d-flex flex-row">
0 commit comments