Forms Symfony2表单提交后刷新同一页面

Forms Symfony2表单提交后刷新同一页面,forms,symfony,page-refresh,Forms,Symfony,Page Refresh,我有一个表单,它的内容是从数据库创建的 在我的控制器中,我有: /** * @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->isVal

我有一个表单,它的内容是从数据库创建的

在我的控制器中,我有:

/**
 * @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, ));
}
它会正确地更新数据库上的信息,但不会使用更新的数据再次构建表单。我只需要刷新当前页面,而不是在“isValid()”中返回

我认为这是可能的,也很容易实现,但我没有找到如何做到这一点:/

编辑-以下是更多相关代码:

/**
 * @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, ));
}
我还提供了表单的屏幕截图:


如果用户选择添加新资源,我需要将其保存到DB(这是正确的),但我需要在可用小时列表中看到它,而不需要用户重新加载页面。

您必须将表单链接到请求

$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.
$entity=新实体();
$form=$this->createFormBuilder($entity)
->添加('field1','text')
->添加('field2','date')
->添加('save','submit',数组('label'=>'submit'))
->getForm();

$form->handleRequest($request);//isValid()并在呈现模板时传递此表单。如果您已经这样做了,但没有包含在上面的代码中,请显示更多代码以获得更好的帮助

以下是正确的方法。事件,尽管您有
$projectId
slug,但在
操作
中,您可以传递整个
对象
,在本例中为
项目
。Symfony将为您处理剩下的(获取右侧
项目
实体)

/**
 * @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, ));
}
更新
根据您的编辑,您需要使用javascript进行客户端dom操作。请查看Symfony官方文档中的此链接。在这里,您可以找到一个您试图实现的示例。

我以一种简单(希望正确)的方式解决了此问题

我只是用以下内容替换了isValid()中的“render”:

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

我可以工作,但有人预见这个解决方案会出现问题吗?

更具动态性的方法是:

$request = $this->getRequest();

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

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

您可能需要显示完整的代码。完成后,请查看更新的问题。问题是我的表单是动态创建的。我将用更多信息更新我的帖子。这似乎有点复杂,可能有点过火。我只需要刷新一下:/n您说您不想刷新页面。可能我在我写的第一篇帖子中被误解了“我只需要刷新当前页面”,下面是我写的一条评论“…用户无需重新加载页面“。我的意思是我确实需要刷新,但我不想成为手动执行刷新的用户。你仍然可以使用我发布的代码来实现你想要的。这一点都没有错。这就是我在成功提交表单时重定向的方式。是的,我使用
return$this->redirect($request->getUri())
@Aistis这解决了我的问题。但是有人能指出为什么会发生这种情况吗?