Symfony3:多步骤表格和转发后刷新 [英] Symfony3 : multi-steps form and flush after forward
问题描述
我有一个表格在我的数据库中添加一个新的开药者。第一步是告知开处方者的各种信息。然后,我会在添加之前检查是否有类似的预定者(第二步是第二种形式),如果有,我要求用户确认。
总之,我有一个1步表单或2个步骤的表单,具体取决于重复。
我尝试过使用CraueFormFlowBundle,但我不知道如何实现我的条件第二步。我的测试没有结果。所以我决定在我的控制器中使用forward方法,并且我喜欢它!
但是,我无法在第2步结束时刷新我的开处理程序(在转发之后),我有这个错误:无法猜测如何从参数prescriber的请求信息中获得Doctrine实例。
addAction(=步骤1)
/ **
*添加一个新的处方器
*
* @Route(/ prescribers / add,name =prescriber_add)
* /
public function addAction(Request $ request){
$ em = $ this-> getDoctrine() - > getManager();
$ rp = $ em-> getRepository('AppBundle:Prescriber');
$ p = new Prescriber();
//建立表格
$ form = $ this-> createForm(AddPrescriberType :: class,$ p);
//处理提交
$ form-> handleRequest($ request);
if($ form-> isSubmitted()&& $ $ form-> isValid()){
#如果开处方器已经存在,则搜索
$ qb = $ rp-> ; createQueryBuilder( 'p');
$ qb->其中($ qb-> expr() - > eq('p.rpps',':rpps'))
- > orWhere($ qb-> expr() - >和X(
$ qb-> expr() - > like('p.lastname',':name'),
$ qb-> expr() - > like('p.firstname',':firstname')
))
- > setParameter('rpps',$ p-> getRpps())
- > setParameter('name','%'。$ p-> getLastname()。'%')
- > setParameter('firstname','%'。$ p-> getFirstname()。' %');
$ duplicates = $ qb-> getQuery() - > getArrayResult();
#有重复的
if(!empty($ duplicates)){
$ em-> persist($ p);
//确认添加了新的处方器
$ params = array('prescriber'=> $ p,'duplicates'=> $ duplicates);
$ query = $ request-> query-> all();
return $ this-> forward('AppBundle:Prescriber:addConfirm',$ params,$ query);
} else {
$ em-> persist($ p); #保存处方器
$ em-> flush(); #更新数据库
$ this-> addFlash('p_success','处方器已成功创建');
返回$ this-> redirectToRoute('prescriber');
$ show
return $ this-> render('prescribers / form-step1.html.twig',array(
'form'=> $ form-> createView()
));
}
addConfirmAction(=步骤2)
/ **
*确认添加了一个新的处方器
*
* @Route(/ prescribe / add / confirm,name =prescriber_add_confirm)
* /
public function addConfirmAction(Prescriber $ prescriber,$ duplicates,Request $ request){
$ em = $ this-> ; getDoctrine() - > getManager();
$ form = $ this-> createFormBuilder() - > getForm();
$ b $ if if($ form-> handleRequest($ request) - > isValid()){
$ em-> persist($ prescriber);
$ em-> flush();
$ this-> addFlash('p_success','Prescriber已成功创建');
返回$ this-> redirectToRoute('prescriber');
}
//显示确认页面
返回$ this-> render('prescribers / form-step2.html.twig',array(
'h1_title '=>'Ajouter un nouveau prescripteur',
'form'=> $ form-> createView(),
'p'=> $开发者,
'duplicates' => $重复
));
}
我认为问题来自于我有2个表单提交的事实。 。
我使用会话找到了解决方案。
(我知道这不是一个完美的方式,但我没有找到其他的)
对于Symfony 3.3。*
$ b $ pre $ 使用Symfony \ Component \HttpFoundation\Session\SessionInterface;
public function addAction(Request $ request,SessionInterface $ session){
// [...]
#有重复的
if( !$ empty $($ duplicates)){
$ data = $ form-> getData();
$ session-> set('prescriber',$ data);
$ session-> set('duplicates',$ duplicates);
返回$ this-> forward('AppBundle:Prescriber:addConfirm');
// [...]
}
public function addConfirmAction(Request $ request,SessionInterface $ session){
$ em = $ this - > getDoctrine() - > getManager();
$ p = $ session-> get('prescriber');
$ duplicates = $ session-> get('duplicates');
//只有CSRF字段的空表单
$ form = $ this-> createFormBuilder() - > getForm(); $
$ b if($ form-> handleRequest($ request) - > isValid()){
$ em-> persist($ p);
$ em-> flush();
$ this-> addFlash('p_success','处方器已成功创建');
返回$ this-> redirectToRoute('prescriber');
}
//显示确认页面
返回$ this-> render('prescribers / form-step2.html.twig',array(
'form '=> $ form-> createView(),
'prescriber'=> $ p,
'duplicates'=> $ duplicates
));
}
I have a form to add a new prescriber in my database. The first step consists in informing the various informations about the prescriber.
Then, I check if there are similar prescribers before adding it (2nd step with a 2nd form) and if there are, I ask the user to confirm.
In short, I have a 1-step form or a 2-steps form, depending on duplicates.
I tried with CraueFormFlowBundle but I don't know how to implement my conditional second step. My tests were inconclusive. So I decided to use forward method in my controller, and I like it !
But, I can't manage to flush my prescriber at the end of the 2nd step (after forwarding), I have this error : Unable to guess how to get a Doctrine instance from the request information for parameter "prescriber".
addAction (= step 1)
/**
* Add a new prescriber
*
* @Route("/prescribers/add", name="prescriber_add")
*/
public function addAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$rp = $em->getRepository('AppBundle:Prescriber');
$p = new Prescriber();
// build the form
$form = $this->createForm(AddPrescriberType::class, $p);
// handle the submit
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
# search if a prescriber already exists
$qb = $rp->createQueryBuilder('p');
$qb->where($qb->expr()->eq('p.rpps', ':rpps'))
->orWhere($qb->expr()->andX(
$qb->expr()->like('p.lastname', ':name'),
$qb->expr()->like('p.firstname', ':firstname')
))
->setParameter('rpps', $p->getRpps())
->setParameter('name', '%'.$p->getLastname().'%')
->setParameter('firstname', '%'.$p->getFirstname().'%');
$duplicates = $qb->getQuery()->getArrayResult();
# there are duplicates
if (!empty($duplicates)) {
$em->persist($p);
// confirm the addition of the new prescriber
$params = array('prescriber' => $p, 'duplicates' => $duplicates);
$query = $request->query->all();
return $this->forward('AppBundle:Prescriber:addConfirm', $params, $query);
} else {
$em->persist($p); # save the prescriber
$em->flush(); # update database
$this->addFlash('p_success', 'The prescriber has been created successfully');
return $this->redirectToRoute('prescriber');
}
}
// show form
return $this->render('prescribers/form-step1.html.twig', array(
'form' => $form->createView()
));
}
addConfirmAction (= step 2)
/**
* Confirm addition of a new prescriber
*
* @Route("/prescribers/add/confirm", name="prescriber_add_confirm")
*/
public function addConfirmAction(Prescriber $prescriber, $duplicates, Request $request) {
$em = $this->getDoctrine()->getManager();
$form = $this->createFormBuilder()->getForm();
if ($form->handleRequest($request)->isValid()) {
$em->persist($prescriber);
$em->flush();
$this->addFlash('p_success', 'Prescriber has been created successfully');
return $this->redirectToRoute('prescriber');
}
// show confirm page
return $this->render('prescribers/form-step2.html.twig', array(
'h1_title' => 'Ajouter un nouveau prescripteur',
'form' => $form->createView(),
'p' => $prescriber,
'duplicates'=> $duplicates
));
}
I think the problem comes from the fact that I have 2 forms submissions...
I found a solution by using the session. (I know it's not a perfect way but I didn't find other one)
For Symfony 3.3.*
use Symfony\Component\HttpFoundation\Session\SessionInterface;
public function addAction(Request $request, SessionInterface $session) {
// [...]
# there are duplicates
if (!empty($duplicates)) {
$data = $form->getData();
$session->set('prescriber', $data);
$session->set('duplicates', $duplicates);
return $this->forward('AppBundle:Prescriber:addConfirm');
// [...]
}
public function addConfirmAction(Request $request, SessionInterface $session) {
$em = $this->getDoctrine()->getManager();
$p = $session->get('prescriber');
$duplicates = $session->get('duplicates');
// empty form with only a CSRF field
$form = $this->createFormBuilder()->getForm();
if ($form->handleRequest($request)->isValid()) {
$em->persist($p);
$em->flush();
$this->addFlash('p_success', 'The prescriber has been created successfully');
return $this->redirectToRoute('prescriber');
}
// show confirm page
return $this->render('prescribers/form-step2.html.twig', array(
'form' => $form->createView(),
'prescriber'=> $p,
'duplicates'=> $duplicates
));
}
这篇关于Symfony3:多步骤表格和转发后刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!