Ser*_*gri 4 forms page-refresh symfony
我有一个表单,其内容是从数据库创建的.
在我的控制器中我有:
/**
* @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, ));
}
Run Code Online (Sandbox Code Playgroud)
它正确地更新了DB上的信息,但它不会再次构建具有更新数据的表单.而不是"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, ));
}
Run Code Online (Sandbox Code Playgroud)
我还包括表单的截图:

如果用户选择添加新资源,我需要将其保留到DB(并且已正确完成),但我需要在可用HR列表中看到它,而无需用户重新加载页面.
Ais*_*tis 12
更有活力的方式是:
$request = $this->getRequest();
return $this->redirectToRoute($request->get('_route'), $request->query->all());
Run Code Online (Sandbox Code Playgroud)
或者干脆
return $this->redirect($request->getUri());
Run Code Online (Sandbox Code Playgroud)
您必须将表格链接到请求。
$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.
Run Code Online (Sandbox Code Playgroud)
仅此后,您测试 $form->isValid() 并在渲染模板时传递此表单。如果您已经执行此操作但尚未包含在上面的代码中,请显示更多代码以获得更好的帮助。
| 归档时间: |
|
| 查看次数: |
18491 次 |
| 最近记录: |