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

时间:2014-12-27 13:58:59

标签: forms symfony page-refresh

我有一个表单,其内容是从DB创建的。

在我的控制器里我有:

/**
 * @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, ));
}

它会正确更新数据库上的信息,但它不会再次构建包含更新数据的表单。而不是在#34; 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, ));
}

我还包括表格的截图: enter image description here

如果用户选择添加新资源,我需要将其保留到DB(并且已正确完成)但是我需要在可用HR列表中看到它,而无需用户重新加载页。

4 个答案:

答案 0 :(得分:12)

更有活力的方式是:

$request = $this->getRequest();

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

或只是

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

答案 1 :(得分:3)

我设法用一种简单的(希望是正确的)方式来解决它。

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

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

我的工作,但有人预见到这个解决方案的问题吗?

答案 2 :(得分:0)

您必须将表单链接到请求。

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

之后,您只需测试$ form-&gt; isValid()并在渲染模板时传递此表单。如果您已经执行此操作并且未包含在上面的代码中,请显示更多代码以获得更好的帮助。

答案 3 :(得分:0)

这是正确的方法。事件虽然您有$projectId slug,但在Action中,您可以传递整个Object,在这种情况下Project。 Symfony将负责其余部分(为您提取正确的Project实体。

/**
 * @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官方文档embedded forms查看此链接。在这里,您将找到一个您正在尝试完成的示例。