Symfony2,与CraueFormFlowBundle形成多个步骤

时间:2015-10-19 12:52:14

标签: symfony craueformflow

我尝试使用CraueFormFlowBundle构建一个多步骤表单。目前,我可以转到第一步(用户信息),但是当我点击" NEXT"按钮。

我的第一步是基于我的用户实体的表单数据类型,第二步是基于实体Etablissement(另一个实体)的另一种表单数据类型。

我的错误信息是:

表单的视图数据应该是AppBundle \ Entity \ Etablissement类的实例,但是是AppBundle \ Entity \ User类的实例。您可以通过设置" data_class"来避免此错误。 null的选项或添加视图转换器,将类AppBundle \ Entity \ User的实例转换为AppBundle \ Entity \ Etablissement的实例。

文件配置步骤:

namespace AppBundle\Form;

// AppBundle/Form/RegistrationEtablissementFlow.php
use Craue\FormFlowBundle\Form\FormFlow;
use Craue\FormFlowBundle\Form\FormFlowInterface;
use Symfony\Component\Form\FormTypeInterface;

class EnregistrementCompletFlow extends FormFlow {

    /**
     * @var FormTypeInterface
     */
    protected $formType;

    public function setFormType(FormTypeInterface $formType) {
        $this->formType = $formType;
    }

    public function getName() {
        return 'enregistrementComplet';
    }

    protected function loadStepsConfig() {
        return array(
            array(
                    'label' => 'Infos personnelles',
                    'form_type' => 'homes_user_registration', // the user's form registered as sevice
            ),
            array(
                    'label' => 'Infos établissement',
                    'form_type' => 'likabee_form_etablissement', // the etablissement's form registered as sevice
                    //'form_type' => $this->formType,
            ),
            array(
                    'label' => 'confirmation',
            ),
        );
    }
}

在我的控制器中:

/**
     * @Route("/registration-etablissement", name="registrationEtablissement")
     */
    public function registrtationEtablissementAction()
    {
        $formData = new User();
        $formData->setEtablissement( new Etablissement());

        $flow = $this->get('likabee.form.flow.enregistrementComplet'); // must match the flow's service id
        $flow->bind($formData);

        // form of the current step
        $form = $flow->createForm();
        if ($flow->isValid($form))
        {
            $flow->saveCurrentStepData($form);

            if ($flow->nextStep())
            {

                // form for the next step
                $form = $flow->createForm();
            }
            else
            {
                // flow finished

                $em = $this->getDoctrine()->getManager();
                $em->persist($formData);
                $em->flush();

                $flow->reset(); // remove step data from the session

                return $this->redirect($this->generateUrl('index')); // redirect when done
            }
        }

        return $this->render('registrationEtablissement.html.twig', array(
                'form' => $form->createView(),
                'flow' => $flow,
        ));
    }

我认为,当我进入第二步时,表单会等待用户类型,而不是Etablissement类型,但我不知道如何解决此问题...

提前致谢...

1 个答案:

答案 0 :(得分:2)

据我所知:

CraueFormFlowBundle的目的是通过多个步骤(即网页)分发一个实体的表单。在这里查看他们的示例代码:https://github.com/craue/CraueFormFlowBundle/blob/master/README.md#create-an-action - $flow->bind($formData);表示实体绑定到整个流程。

可能的解决方案: