@@ -212,7 +212,6 @@ Using an event listener, your form might look like this::
212212 use Symfony\Component\Form\FormBuilderInterface;
213213 use Symfony\Component\Form\FormEvents;
214214 use Symfony\Component\Form\FormEvent;
215- use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
216215 use Symfony\Component\Form\Extension\Core\Type\TextType;
217216 use Symfony\Component\Form\Extension\Core\Type\TextareaType;
218217
@@ -236,28 +235,30 @@ contains only this user's friends.
236235Luckily it is pretty easy to inject a service inside of the form. This can be
237236done in the constructor::
238237
239- private $tokenStorage;
238+ use Symfony\Component\Security\Core\Security;
239+ // ...
240+
241+ private $security;
240242
241- public function __construct(TokenStorageInterface $tokenStorage )
243+ public function __construct(Security $security )
242244 {
243- $this->tokenStorage = $tokenStorage ;
245+ $this->security = $security ;
244246 }
245247
246248.. note ::
247249
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.
250+ You might wonder, now that you have access to the User, why not just use it
251+ directly in ``buildForm() `` and omit the event listener? This is because
252+ doing so in the ``buildForm() `` method would result in the whole form type
253+ being modified and not just this one form instance. This may not usually be
254+ a problem, but technically a single form type could be used on a single
255+ request to create many forms or fields.
255256
256257Customizing the Form Type
257258~~~~~~~~~~~~~~~~~~~~~~~~~
258259
259- Now that you have all the basics in place you can take advantage of the `` TokenStorageInterface ``
260- and fill in the listener logic::
260+ Now that you have all the basics in place you can use the features of the
261+ security helper to fill in the listener logic::
261262
262263 // src/AppBundle/Form/Type/FriendMessageFormType.php
263264
@@ -266,16 +267,16 @@ and fill in the listener logic::
266267 use Symfony\Bridge\Doctrine\Form\Type\EntityType;
267268 use Symfony\Component\Form\Extension\Core\Type\TextType;
268269 use Symfony\Component\Form\Extension\Core\Type\TextareaType;
269- use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ;
270+ use Symfony\Component\Security\Core\Security ;
270271 // ...
271272
272273 class FriendMessageFormType extends AbstractType
273274 {
274- private $tokenStorage ;
275+ private $security ;
275276
276- public function __construct(TokenStorageInterface $tokenStorage )
277+ public function __construct(Security $security )
277278 {
278- $this->tokenStorage = $tokenStorage ;
279+ $this->security = $security ;
279280 }
280281
281282 public function buildForm(FormBuilderInterface $builder, array $options)
@@ -286,7 +287,7 @@ and fill in the listener logic::
286287 ;
287288
288289 // grab the user, do a quick sanity check that one exists
289- $user = $this->tokenStorage->getToken() ->getUser();
290+ $user = $this->security ->getUser();
290291 if (!$user) {
291292 throw new \LogicException(
292293 'The FriendMessageFormType cannot be used without an authenticated user!'
0 commit comments