Skip to content

Commit 2d1cecb

Browse files
committed
minor #10061 Fix How to dynamically Generate Forms Based on user Data (yceruto)
This PR was merged into the 2.8 branch. Discussion ---------- Fix How to dynamically Generate Forms Based on user Data Fix #6376 Better diff https://github.com/symfony/symfony-docs/pull/10061/files?utf8=%E2%9C%93&diff=unified&w=1 Commits ------- f312d21 Fix How to dynamically Generate Forms Based on user Data
2 parents 03a8055 + f312d21 commit 2d1cecb

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

form/dynamic_form_modification.rst

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,6 @@ done in the constructor::
244244
$this->tokenStorage = $tokenStorage;
245245
}
246246

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-
257247
Customizing the Form Type
258248
~~~~~~~~~~~~~~~~~~~~~~~~~
259249

@@ -294,38 +284,41 @@ and fill in the listener logic::
294284
);
295285
}
296286

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;
318292
}
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+
});
320309
}
321310

322311
// ...
323312
}
324313

325314
.. note::
326315

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

330323
Using the Form
331324
~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)