Symfony2确认表格提交
我需要能够在表单数据持久化到数据库之前对其执行操作。问题是,操作可能有风险,每次我都需要用户的同意 我希望通过一个确认表单(带有一条解释发生了什么的小消息)来实现这一点,而不是通过Javascript确认窗口 如何实现此功能 以下是处理表单的控制器操作方法的示例:Symfony2确认表格提交,symfony,symfony-2.2,Symfony,Symfony 2.2,我需要能够在表单数据持久化到数据库之前对其执行操作。问题是,操作可能有风险,每次我都需要用户的同意 我希望通过一个确认表单(带有一条解释发生了什么的小消息)来实现这一点,而不是通过Javascript确认窗口 如何实现此功能 以下是处理表单的控制器操作方法的示例: <?php public function indexAction(Request $request) { ... $form = $this->createFormBuilder($myEntity)
<?php
public function indexAction(Request $request)
{
...
$form = $this->createFormBuilder($myEntity)
->add('someField', 'integer', array('required' => true))
// Lots and lots of fields
->getForm();
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
// CUSTOM VALIDATION HERE
// If invalid, must display a new confirmation form to ask user
// if it's alright to do a somewhat risky operation that would
// validate the form data.
// Else, persist the data.
$db = $this->getDoctrine()->getManager();
$db->persist($myEntity);
$db->flush();
return $this->redirect($this->generateUrl('my_path_to_success_page'));
}
}
return $this->render('MyBundle:Preferences:index.html.twig', array(
'form' => $form->createView(),
'errors' => $form->getErrors(),
));
}
您可能需要查看,它允许您创建多步骤表单