These are my first steps on SF2. I want to set up a multi step form on an entity which contains other entities.
I've a form type (shortened)
class ApplicationFormType extends AbstractType
{
protected $_step = 1;
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($this->_step == 1) {
$builder
->add('patient', new Type\PersonType())
->add('insurance', 'entity', array(
'class' => 'Acme\MainBundle\Entity\Insurance',
));
} elseif ($this->_step == 2) {
$nurse = new Type\PersonType();
$nurse->requireBareData();
$builder
->add('nurse', $nurse)
->add('nurse_type', 'entity', array(
'class' => 'Acme\MainBundle\Entity\NurseType',
'expanded' => true,
'multiple' => false,
))
->add('nursing_support', 'checkbox', array(
'required' => false,
));
}
}
public function setStep($step)
{
$this->_step = $step;
}
My controller looks like
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,
));
}
My question is, if I had to "copy" the properties of first form step seperately, like:
$data->setPatient($step_1->getPatient());
$data->setInsurance($step_1->getInsurance());
or can I merge the unserialized data from session with data from second form step? Or is there a completely different approach?