How to make a form in some steps in Symfony2 - Verifying a step

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 { /** * @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 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.

+4
source share
1 answer

After binding your object with MyClass1Type, your entity has a valid first name, but not a last name. $myFormClass1->isValid() returns false because it is trying to validate an object, and you did not specify to validate part of the data, therefore it does not like the last name to be zero.

You must use validation groups to validate your entity against partial data. Check here in the symfony book.

Add to your form:

 public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'validation_groups' => array('validationStep1'), )); } 

And define your validation group in the @Assert annotation on your entity using @Assert\NotNull(groups={"validationStep1"}) :

 /** * @var string * * @ORM\Column(name="name", type="string", length=255) * @Assert\NotNull(groups={"validationStep1"}) */ private $name; /** * @var string * * @ORM\Column(name="surname", type="string", length=255) */ private $surname; 
+6
source

Source: https://habr.com/ru/post/1492736/


All Articles