Skip to content

Fix How to dynamically Generate Forms Based on user Data #10061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 27 additions & 34 deletions form/dynamic_form_modification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,6 @@ done in the constructor::
$this->tokenStorage = $tokenStorage;
}

.. note::

You might wonder, now that you have access to the User (through the token
storage), why not just use it directly in ``buildForm()`` and omit the
event listener? This is because doing so in the ``buildForm()`` method
would result in the whole form type being modified and not just this
one form instance. This may not usually be a problem, but technically
a single form type could be used on a single request to create many forms
or fields.

Customizing the Form Type
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -294,38 +284,41 @@ and fill in the listener logic::
);
}

$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($user) {
$form = $event->getForm();

$formOptions = array(
'class' => User::class,
'choice_label' => 'fullName',
'query_builder' => function (EntityRepository $er) use ($user) {
// build a custom query
// return $er->createQueryBuilder('u')->addOrderBy('fullName', 'DESC');

// or call a method on your repository that returns the query builder
// the $er is an instance of your UserRepository
// return $er->createOrderByFullNameQueryBuilder();
},
);

// create the field, this is similar the $builder->add()
// field name, field type, data, options
$form->add('friend', EntityType::class, $formOptions);
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($user) {
if (null !== $event->getData()->getFriend()) {
// we don't need to add the friend field because
// the message will be addressed to a fixed friend
return;
}
);

$form = $event->getForm();

$formOptions = array(
'class' => User::class,
'choice_label' => 'fullName',
'query_builder' => function (UserRepository $userRepository) use ($user) {
// call a method on your repository that returns the query builder
// return $userRepository->createFriendsQueryBuilder($user);
},
);

// create the field, this is similar the $builder->add()
// field name, field type, field options
$form->add('friend', EntityType::class, $formOptions);
});
}

// ...
}

.. note::

The ``multiple`` and ``expanded`` form options will default to false
because the type of the friend field is ``EntityType::class``.
You might wonder, now that you have access to the ``User`` object, why not
just use it directly in ``buildForm()`` and omit the event listener? This is
because doing so in the ``buildForm()`` method would result in the whole
form type being modified and not just this one form instance. This may not
usually be a problem, but technically a single form type could be used on a
single request to create many forms or fields.

Using the Form
~~~~~~~~~~~~~~
Expand Down