I would like doing a form in some steps in Symfony2 (2.3 exactly), but when I try to do this, I get an error in my form.
I have done the next:
1) I've created a class
class MyClass
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @Assert\NotNull()
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="surname", type="string", length=255)
* @Assert\NotNull()
*/
private $surname;
}
2) I've 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 have created 2 more classes to separate the process for getting the data of the form:
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 some 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 render the first form (new1Action) and it get the data perfectly, but the problem is when I submit the data. In the new2Action, the application goes throw the response("error") code, because the form is not valid. The print_r() function shows the next 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 that the problem is that the class is not complete with the data got in the first form, but I need to separate the form in two steps and I have no idea how deal with this error.
Could someone help me?
Thanks in advance.