当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Make changing form (like draft/finish) on symfony2

问题描述:

I want to make forms

which has three scenes

1.input view (which has 'confirm' button)

2.draft check view (which has 'send' button)

3.send view

in method 1 you can input the data then click 'confirm' button

the system write the data in DB as draft.

in method 2 you see the data and confirm then push 'send' button

the system write the flg 'confirmed'

these are my code.

it works well method 1 but if I push 'send' button in method2.

it doesn't go.

if ($form->isValid()) {

My idea is something wrong??

public function writeEvalStudentAction(Request $request,$keyStr){

...

$form = $this->createFormBuilder($evalStudent)

->add('commentToStudent')

->add('confirm','submit')->getForm();

$form->handleRequest($request);

if ($form->isValid()) {

if ($form->has('send')){

if ($form->get('send')->isClicked()){

//set confirm flg then shows thanks screen.

$evalStudent->setConfirmed(true);

$em->persist($evalStudent);

$em->flush();

return $this->render('AcmeMemberBundle:Default:confirmedEvalStudent.html.twig');

}

}

if ($form->has('confirm')){

if ($form->get('confirm')->isClicked()){

// write in the db as draft.

$evalStudent->setCommentToStudent($form->get('commentToStudent')->getData());

$em->persist($evalStudent);

$em->flush();

$form = $this->createFormBuilder($evalStudent)->add('send','submit')->getForm();

return $this->render('AcmeMemberBundle:Default:checkEvalStudent.html.twig',

array('form' => $form->createView()));

}

}

return $this->render('AcmeMemberBundle:Default:writeEvalStudent.html.twig',

array('form' => $form->createView()));

}

网友答案:

Split it up in 4 controller actions with 4 separate routes. Whereas route2 and route3 require the id of your draft object in the routing parameters.

Make the form1 from step 1 point to step2. In step2 validate the form and if it is ok insert to db and get your id, then show form2 which points to step 3, if not show form1 with form errors.

Same for step2->step3 and step3->step4.

In step 4 you convert your draft to a final object and persist it and remove the draft.

分享给朋友:
您可能感兴趣的文章:
随机阅读: