Symfony2: form submission issue

Hi everyone, I have some problems with Symfony2 FormBuilder, in fact, I have a user entity that is associated with (OneByOne) with the Adress entity, it seems very simple, but when I try to implement the AddressType Form in UserType One I encounter this exception :

The form view data is expected to be an instance of the Acme \ Bundle \ AddressBundle \ Entity \ Adresse class, but is an instance of the Doctrine \ Common \ Collections \ ArrayCollection class. You can avoid this error by setting the "data_class" option to zero or by adding a view transformer that converts an instance of the Doctrine \ Common \ Collections \ ArrayCollection class to an Acme \ Bundle \ AddressBundle \ Entity \ Adresse

I put some code here (shortened for reading) to make my problem more understandable:

My User class (which extends FosUserBundle):

class User extends BaseUser { ... /** * @ORM\OneToOne(targetEntity="Acme\bundle\AddressBundle\Entity\Address", cascade={"persist", "remove"}) * @ORM\JoinColumn(nullable=true) * @Assert\Valid */ public $address; ....... } 

Associated Form Type buildForm function:

  public function buildForm(FormBuilderInterface $builder, array $options) { parent::buildForm($builder, $options); // add your custom field $builder->add('name','text') ->add('address',new AddressType(),array( 'data_class' => 'Acme\Bundle\AddressBundle\Entity\Address' ) ); } 

My address form type is:

  public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('city','text') ->add('title','text'); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Acme\Bundle\AddressBundle\Entity\Address' )); } 

Thank you in advance for your help!

+6
source share
2 answers

One problem is that you have made the address public. In Doctrine 2, you need to make your properties private or protected. D2 relies on this to implement lazy loading. Basically, your address never loads, although why you get an array is a puzzle. Are you initializing the address as an array in your constructor? Perhaps a copy / paste error?

You must have:

 class User extends BaseUser { protected $address; public function getAddress() { return $this->address; } 

You also need to make sure that the UserObject always has an Address object, or that the form will complain.

==================================================== ======

Looking at my pate, I see:

 class User extends BaseUser { public function __construct() { $this->adresse = new \Doctrine\Common\Collections\ArrayCollection(); } 

Which not only explains the unwanted array, but also spoils the base class of the user fos, since it is not called by the constructor.

I would suggest that you split your forms to the minimum user / address and get the job done. Then add your villi and everything else. In fact, just start by creating a simple user, and then add an address.

You did not show how you created the user object, but remember that it is up to you to ensure that the address object exists before the form begins. Teaching will not create it for you.

+1
source

Your address is an object, so you need to tell your form that the field type is loaded from the object. See here: http://symfony.com/doc/2.2/reference/forms/types/entity.html

Try something like this:

 $builder->add('name', 'text') ->add('address', 'entity',array( 'class' => 'AddressBundle:Address', 'property' => 'address' )); 
0
source

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


All Articles