Symfony2确认表单提交

时间:2013-06-07 03:14:02

标签: symfony symfony-2.2

我需要能够在表单数据持久化到数据库之前对其进行操作。问题是,操作可能有风险,每次都需要用户的同意。

我想通过确认表(通过一条消息说明正在发生的事情)来做到这一点,而不是使用Javascript确认窗口。

如何实现此功能?

以下是处理表单的控制器操作方法的示例:

<?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(),
    ));
}

1 个答案:

答案 0 :(得分:0)

您可能需要查看CraueFormFlowBundle,它允许您创建多步骤表单。

相关问题