Forms Symfony2将多步骤表单合并为一个结果

Forms Symfony2将多步骤表单合并为一个结果,forms,symfony,entity,symfony-2.2,Forms,Symfony,Entity,Symfony 2.2,这是我在SF2上的第一步。我想在包含其他实体的实体上设置一个多步骤表单 我有一个表单类型(缩写) 我的控制器看起来像 public function assistAction() { $request = $this->getRequest(); $step = $this->getSession()->get('assist_step', 1); if ($request->getMethod() != 'POST') { $s

这是我在SF2上的第一步。我想在包含其他实体的实体上设置一个多步骤表单

我有一个表单类型(缩写)

我的控制器看起来像

public function assistAction() {
    $request = $this->getRequest();

    $step = $this->getSession()->get('assist_step', 1);
    if ($request->getMethod() != 'POST') {
        $step = 1;
        $this->getSession()->set('assist_data', NULL);
    }
    $this->getSession()->set('assist_step', $step);
    $application = new Application();
    $type = new ApplicationFormType();
    $type->setStep($step);

    $form = $this->createForm($type, $application, array(
        'validation_groups' => array($step == 1 ? 'FormStepOne' : 'FormStepTwo'),
    ));
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        if ($form->isValid()) {
            if ($step == 1) {

                $data = $form->getData();
                $this->getSession()->set('assist_data', serialize($data));
                $this->getSession()->set('assist_step', ++$step);
                $type = new ApplicationFormType();
                $type->setStep($step);
                $form = $this->createForm($type, $application, array(
                    'validation_groups' => array('FormStepTwo'),
                ));

            } else {

                $step_1 = unserialize($this->getSession()->get('assist_data', ''));
                $data = $form->getData();
=>                $data->setPatient($step_1->getPatient());
=>                $data->setInsurance($step_1->getInsurance());
                $this->getSession()->set('assist_data', NULL);
                $this->getSession()->set('assist_step', 1);

            }
        }
    }

    return $this->render('Acme:Default:onlineassist.html.twig', array(
        'form' => $form->createView(),
        'step' => $step,
    ));
}
我的问题是,如果我必须单独“复制”第一个表单步骤的属性,例如:

$data->setPatient($step_1->getPatient());
$data->setInsurance($step_1->getInsurance());
或者我可以将会话中未序列化的数据与第二个表单步骤中的数据合并吗?
或者有完全不同的方法吗?

可以根据上的响应序列化实体并将其放入会话中

如果每个表单都是不同的类型,我建议您将其分为多种类型

以下是我的设想:

public function submitAction($step = 1)
{
    switch ($step) {
        case 1:
            $entity = new EntityOne();
            $form = $this->createForm(new EntityOneType(), $entity);
            $template = "template_one.html.twig";
            $redirect = $urlToStepTwo; // which redirects to this controller action with $step = 2
            break;
        case 2:
            $entity = new EntityTwo();
            $form = $this->createForm(new EntityTwoType(), $entity);
            $template = "template_two.html.twig";
            $redirect = $urlToStepThree; // which redirects to this controller action with $step = 3
            break;
        case 3:
            $entity = new EntityThird();
            $form = $this->createForm(new EntityThreeType(), $entity);
            $template = "template_three.html.twig";
            $redirect = $urlToConfirmPage; // which redirects to another controller action that unserialize all entities and save them in the DB
            break;
        default:
            throw $this->createNotFoundException();
            break;
    }

    $request = $this->get('request');
    $session = $this->get('session');

    if ($request->isMethod('post') === true) {

        $form->bind($request);

        if ($form->isValid() === true) {
            $session->set('entity_'. $step, serialize($entity));

            return $this->redirect($redirect);
        }
    }

    $params = array(
        'form'    => $form->createView(),
        'entity'  => $entity
    );

    return $this->render($template, $params);
}

好的,除了单独设置pre-step字段,没有其他方法,比如

$pre_step = unserialize($this->getSession()->get('assist_data', ''));
$data->setPatient($pre_step->getPatient());
$data->setInsurance($pre_step->getInsurance());
$data->setInsuranceNumber($pre_step->getInsuranceNumber());
$data->setInsuranceLevel($pre_step->getInsuranceLevel());

这就是我到目前为止所做的。

多步骤表单的最终目的是什么?您希望在整个过程中保留提交的数据(如果数据有效),并且在客户端审查数据后,将其注入数据库,对吗?@ThomasPotaire:不完全正确。客户没有查看数据。如您所见(在FormType中),第二步提供了另一个由用户填写的实体字段。(还有第三步。)当用户完成所有步骤时(顺便说一句:第二步很快将是可选的),包含所有相关实体的
应用程序
实体应该保存到数据库中。您的解决方案当然适用于单独的实体。但我在三个步骤中的两个步骤中使用了一个大实体。
ApplicationFormType
表示一个
Application
实体。(顺便说一句:您的代码不符合SF2.2,即
bindRequest
vs
bind
getMethod()
vs
isMethod('post')
)您可以创建两种表单类型,表示
应用程序的一组数据,然后将它们进行融合(感谢不推荐的调用)。这正是我要做的(看一看我的帖子)。这正是我的问题。如何合并这两个步骤。可以用PHP中的一条语句完成,还是让我在第二步手动设置第一步的每个属性?
$pre_step = unserialize($this->getSession()->get('assist_data', ''));
$data->setPatient($pre_step->getPatient());
$data->setInsurance($pre_step->getInsurance());
$data->setInsuranceNumber($pre_step->getInsuranceNumber());
$data->setInsuranceLevel($pre_step->getInsuranceLevel());