Symfony2, form multi steps with CraueFormFlowBundle

2.7k views Asked by At

I'm trying to build a multi-steps form with CraueFormFlowBundle. For the moment, I can go to the first step (users information), but I have an error when I click on the "NEXT" button.

My first step is a form data type based on my User entity, and the second step is another form data type based on entity Etablissement (another entity).

There is my error message :

The form's view data is expected to be an instance of class AppBundle\Entity\Etablissement, but is an instance of class AppBundle\Entity\User. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class AppBundle\Entity\User to an instance of AppBundle\Entity\Etablissement.

The file configuration steps :

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

In my controller :

/**
     * @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,
        ));
    }

I think that when i'm going to the second step, the form waiting a User type, and not an Etablissement type, but I don't know how to resolve this ...

Thanks in advance ...

1

There are 1 answers

0
Thomas Landauer On

As far as I understand it:

The purpose of CraueFormFlowBundle is to distribute the form for one entity over multiple steps (i.e. web pages). See their example code here: https://github.com/craue/CraueFormFlowBundle/blob/master/README.md#create-an-action - $flow->bind($formData); means that the entity is bound to the entire flow.

Possible solutions for you: