Symfony2 form refresh same page after submit

感情迁移 提交于 2020-01-03 17:29:13

问题


I have a form whose content is created from a DB.

in my controller i have:

/**
 * @Route("/HR/manage/{projectID}", name="hr_manage")
 */
public function manageHRAction(Request $request, $projectID)
{
//here I get all the data from DB and create the form
if ($form->isValid()) 
    {
    //here I do all the relevant changes in the DB
    return $this->render('HR/show.html.twig', array('hrlist' => $HRsInMyDomain, 'form' => $form->createView(), 'HRs' => $HRsInThisProject, 'project' => $prj, ));
    }
return $this->render('HR/show.html.twig', array('hrlist' => $HRsInMyDomain, 'form' => $form->createView(), 'HRs' => $HRsInThisProject, 'project' => $prj, ));
}

It updates the info on the DB properly, but it does not build again the form with updated data. Instead of the return inside the "isValid()" I simply need a refresh on the current page.

I assume it's possible and easy to accomplish, but I failed to find how to do it :/

EDIT - here comes more relevant code:

/**
 * @Route("/HR/manage/{projectID}", name="hr_manage")
 */
public function manageHRAction(Request $request, $projectID)
{
$user = $this->container->get('security.context')->getToken()->getUser(); //get current user
$em = $this->getDoctrine()->getManager(); //connect to DB
$prj = $this->getDoctrine()->getRepository('AppBundle:Project')->findOneById($projectID);
[...]
// here comes some code to generate the list of $HRsInThisProject and the list of roles ($rolesListForForm)
[...]
foreach ($HRsInThisProject as $key => $HR)
    {
    $form->add('roleOf_'.$key, 'choice', array('choices'   => $rolesListForForm, 'required'  => true, 'data' => $HR['role'], 'label' => false, ));
    $form->add('isActive_'.$key, 'choice', array('choices'   => [0 => 'Inactive', 1 => 'Active'] , 'required'  => true, 'data' => $HR['is_active'], 'label' => false, ));
    }
[...]
// here there is some code to get the $HRsInMyDomainForForm
[...]
$form->add('HRid', 'choice', array('choices' => $HRsInMyDomainForForm,'required' => false, 'placeholder' => 'Choose a resource', 'label' => false, ));
$form->add('role', 'choice', array('choices' => $rolesListForForm,'required' => false, 'placeholder' => 'Choose a role', 'label' => false, ));            
$form->add('save', 'submit', array('label' => 'Save'));     

$form->handleRequest($request);

if ($form->isValid()) 
    {
        {
        [...] here there is a huge portion of code that determines if I need to generate a new "event" to be stored, or even multiple events as I can change several form fields at once

        // If I needed to create the event I persist it (this is inside a foreach)
        $em->persist($newHREvent);
        }
    $em->flush();
    return $this->render('HR/show.html.twig', array('projectID' => $prj->getId(), 'hrlist' => $HRsInMyDomain, 'form' => $form->createView(), 'HRs' => $HRsInThisProject, 'project' => $prj, ));
    }
return $this->render('HR/show.html.twig', array('projectID' => $prj->getId(), 'hrlist' => $HRsInMyDomain, 'form' => $form->createView(), 'HRs' => $HRsInThisProject, 'project' => $prj, ));
}

I also include a screenshot of the form:

If a user selects to add a new resouce, I need to persist it to DB (and that is done properly) but then I need to see it in the list of available HRs, without the need for the user to reload the page.


回答1:


More dynamic way would be:

$request = $this->getRequest();

return $this->redirectToRoute($request->get('_route'), $request->query->all());

or simply

return $this->redirect($request->getUri());



回答2:


I managed to solve it in a simple (and I hope correct) way.

I simply substituted the "render" inside the isValid() with the following:

return $this->redirect($this->generateUrl('hr_manage', array('projectID' => $prj->getId())));

I works, but does anybody foresee problems with this solution?




回答3:


You have to link the form to the request.

$entity = new Entity();
$form = $this->createFormBuilder($entity)
    ->add('field1', 'text')
    ->add('field2', 'date')
    ->add('save', 'submit', array('label' => 'Submit'))
    ->getForm();
$form->handleRequest($request); // <= this links the form to the request.

only after that you test $form->isValid() and pass this form when rendering the template. If you already did this and haven't included in the code above please show more code for better help.




回答4:


Here is the right way to do it. Event though you have $projectId slug, in Action you can pass whole Object, in this case Project. Symfony will take care for the rest (fetching right Project entity for you.

/**
 * @Route("/HR/manage/{projectID}", name="hr_manage")
 */
public function manageHRAction(Request $request, Project $project)
{
    $form = $this->createForm(new ProjectType(), $project);
    $form->handleRequest($request);

    // ... your business logic or what ever

    //here I get all the data from DB and create the form
    if ($form->isValid() && $form->isSubmitted()) {
        $em->persist($project);
        // no need to flush since entity already exists
        // no need to redirect
    }

    // here $form->createView() will populate fields with data since you have passed Poroject $project to form
    return $this->render('HR/show.html.twig', array('hrlist' => $HRsInMyDomain, 'form' => $form->createView(), 'HRs' => $HRsInThisProject, 'project' => $prj, ));
}

Update

According to your edit, you need to use javascript for client-side dom manipulation. Check this link from Symfony official document embedded forms. Here you'll find an example of what you're trying to accomplish.



来源:https://stackoverflow.com/questions/27667929/symfony2-form-refresh-same-page-after-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!