I would like to make the form in some steps in Symfony2 (2.3 for sure), but when I try to do this, I get an error in my form.
I have done the following:
1) I created a class
class MyClass { private $id; private $name; private $surname; }
2) I created the FormType class:
class MyClassType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', null, array('label' => 'name')) ->add('surname', null, array('label' => 'surname')); }
And I created 2 more classes to separate the process of obtaining form data:
class MyClass1Type extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', null, array('label' => 'name')); } class MyClass2Type extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('surname', null, array('label' => 'surname')); }
And in the controller, I have several methods:
public function new1Action() { $entity = new MyClass(); $form = $this->createForm( new MyClass1Type( $entity ); return array( 'entity' => $entity, 'form' => $form->createView(), ); } public function new2Action(Request $request) { $entity = new MyClass(); $formMyClass1 = $this->createForm(new MyClass1Type($entity) ); $formMyClass1->bind($request); if (!$formMyClass1->isValid()) { print_r($formMyClass1->getErrors()); return new Response("Error"); } $form = $this->createForm( new MyClass2Type($entity) ); return array( 'entity' => $entity, 'form' => $form->createView(), ); }
I create the first form (new1Action) and receive the data perfectly, but the problem is when I submit the data. In new2Action, the application sends a response code ("error") because the form is invalid. The print_r () function displays the following information:
Array ( [0] => Symfony\Component\Form\FormError Object ( [message:Symfony\Component\Form\FormError:private] => Este valor no deberΓa ser null. [messageTemplate:protected] => This value should not be null. [messageParameters:protected] => Array ( ) [messagePluralization:protected] => ) )
I think the problem is that the class is not filled with data obtained in the first form, but I need to separate the form in two stages, and I have no idea how to deal with this error.
Can someone help me?
Thanks in advance.