I am working with Symfony 3.4
and would like to create a simple form which presents a list entities which can be selected for deletion using checkboxes:
The problem is, that "the first 50 entities" depends on wether entities have been delete / the form has been handled.
choises
class SomeController
{
public function deleteUsersAction(Request $request)
{
$users = $this->loadUsersFromDB(50);
$form = $this->creatFormBuilder()
->add('users', EntityType::class, [
'class' => 'AppBundle:User',
'choices' => $users, // $users are added here
'multiple' => true,
'expanded' => true,
])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$users = $form->getData();
$this->deleteUsers($user);
// Load the now first 50 users
$users = $this->loadUsersFromDB(50);
// How to add these users to the form??
}
return $this->render('AppBundle::user_list.html.twig', ['form' => $form->createView()]);
}
}
How to set the correct users to the form after the submitted users have been deleted?
To answer a question from the comments I provide a small example:
$users = $this->loadUsersFromDB(5)
again to get the correct result, likeThe Problem:
So: Loading the list of users before to form is handled is necessary work, because the list changes when the form is handled.
How can this work be avoided?
Of course loading 5 users as in the example is no big deal, but in real code this might be hundreds of heavy entities and running the same code twice might have a significant performance impact.
I think you did not understood my proposal in the comment, so I'll explain here in more detail /also now I write from the laptop:)/
class SomeController {
public function deleteUsersAction(Request request) {
$users = $this->loadUsersFromDB(50);
$form = $this->creatFormBuilder()
->add('users', EntityType::class, [
'class' => 'AppBundle:User',
'choices' => $users, // $users are added here
'multiple' => true,
'expanded' => true,
])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$users = $form->getData();
$this->deleteUsers($user);
// Load the now first 50 users
$users = $this->loadUsersFromDB(50); <-- you don't need this
// Q: How to add these users to the form??
// A: When you return a redirect response to the same page /you can also pass any query parameters if you had any filters and etc../
// This will ensure that the page is loaded properly with the new list of the users and Bob and Alice will NOT be in the list
// If you don't do it with a redirect and the user submits the form the browser remembers the data and if the users hits the refresh button of the browser it will resubmit the form and with your approach it may delete next 2 users /Mike and John/.
return $this->redirectToRoute('YOUR_ROUTE', $request->query->all());
}
return $this->render('AppBundle::user_list.html.twig', ['form' => $form->createView()]);
}
}```
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.