Skip to content

Commit c458d34

Browse files
committed
minor #9619 Update the dynamic form article to use modern Symfony practices (javiereguiluz)
This PR was squashed before being merged into the 4.0 branch (closes #9619). Discussion ---------- Update the dynamic form article to use modern Symfony practices Commits ------- c45553d Update the dynamic form article to use modern Symfony practices
2 parents e06565d + c45553d commit c458d34

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

form/dynamic_form_modification.rst

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -231,33 +230,31 @@ Using an event listener, your form might look like this::
231230
}
232231

233232
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::
235235

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;
238237

239-
private $tokenStorage;
238+
private $security;
240239

241-
public function __construct(TokenStorageInterface $tokenStorage)
240+
public function __construct(Security $security)
242241
{
243-
$this->tokenStorage = $tokenStorage;
242+
$this->security = $security;
244243
}
245244

246245
.. note::
247246

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.
255253

256254
Customizing the Form Type
257255
~~~~~~~~~~~~~~~~~~~~~~~~~
258256

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::
261258

262259
// src/Form/Type/FriendMessageFormType.php
263260

@@ -266,16 +263,16 @@ and fill in the listener logic::
266263
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
267264
use Symfony\Component\Form\Extension\Core\Type\TextType;
268265
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;
270267
// ...
271268

272269
class FriendMessageFormType extends AbstractType
273270
{
274-
private $tokenStorage;
271+
private $security;
275272

276-
public function __construct(TokenStorageInterface $tokenStorage)
273+
public function __construct(Security $security)
277274
{
278-
$this->tokenStorage = $tokenStorage;
275+
$this->security = $security;
279276
}
280277

281278
public function buildForm(FormBuilderInterface $builder, array $options)
@@ -286,7 +283,7 @@ and fill in the listener logic::
286283
;
287284

288285
// grab the user, do a quick sanity check that one exists
289-
$user = $this->tokenStorage->getToken()->getUser();
286+
$user = $this->security->getUser();
290287
if (!$user) {
291288
throw new \LogicException(
292289
'The FriendMessageFormType cannot be used without an authenticated user!'
@@ -301,13 +298,12 @@ and fill in the listener logic::
301298
$formOptions = array(
302299
'class' => User::class,
303300
'choice_label' => 'fullName',
304-
'query_builder' => function (EntityRepository $er) use ($user) {
301+
'query_builder' => function (EntityRepository $userRepository) use ($user) {
305302
// build a custom query
306-
// return $er->createQueryBuilder('u')->addOrderBy('fullName', 'DESC');
303+
// return $userRepository->createQueryBuilder('u')->addOrderBy('fullName', 'DESC');
307304

308305
// 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();
311307
},
312308
);
313309

@@ -331,11 +327,8 @@ Using the Form
331327

332328
If you're using :ref:`autowire <services-autowire>` and
333329
: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.
339332

340333
In a controller, create the form like normal::
341334

0 commit comments

Comments
 (0)