Show empty form collection item in symfony without javascript

I am trying to display a collection form. The collection should display an empty subform. Due to the nature of the projects, I cannot rely on JavaScript for this.

Googling didn't help, and I don't seem to be working by adding an empty object to the collection field.

What I still have:

public function indexAction($id) { $em = $this->getDoctrine()->getManager(); $event = $em->getRepository('EventBundle:EventDynamicForm')->find($id); $entity = new Booking(); $entity->addParticipant( new Participant() ); $form = $this->createForm(new BookingType(), $entity); return array( 'event' => $event, 'edit_form' => $form->createView() ); } 

In BookingType.php buildForm ()

 $builder ->add('Participants', 'collection') 

In Twig Template

 {{ form_row(edit_form.Participants.0.companyName) }} 

If I put the line $ entity-> addParticipant (new member ()); in indexAction () I get an error:

It is expected that data of the form view will be of the scalar, array, or instance \ ArrayAccess types, but is an instance of the Yanic \ EventBundle \ Entity \ Member class. You can avoid this error by setting the "data_class" option for "Yanic \ EventBundle \ Entity \ Partant" or by adding a view transformer that converts an instance of the Yanic \ EventBundle \ Entity \ Member class to a scalar, array, or instance from \ ArrayAccess.

If I delete the specified line, Twig complains:

The method "0" for the object "Symfony \ Component \ Form \ FormView" does not exist in / Applications / MAMP / htdocs / symfony -standard-2.1 / src / Yanic / EventBundle / Resources / views / Booking / index.html.twig in the line 27

EDIT: addParticipant is the default method created by the doctrine: generate: entities command

 /** * Add Participants * * @param \Yanic\EventBundle\Entity\Participant $participants * @return Booking */ public function addParticipant(\Yanic\EventBundle\Entity\Participant $participants) { $this->Participants[] = $participants; return $this; } 

I am sure that I am doing something wrong, but I can not find the key: - (

+4
source share
2 answers

I think you have lost a bit in the Symfony2 form collection, although I think you already read http://symfony.com/doc/current/cookbook/form/form_collections.html .
Here, I just focus on the document, help other SO readers, and work a bit on the answer to the question. :)

First, you must have at least two objects. In your case, Booking and Participant . To arrange your reservation, add the following. Since you are using Doctrine, Participant needs to be wrapped in an ArrayCollection .

 use Doctrine\Common\Collections\ArrayCollection; class Booking() { // ... protected $participants; public function __construct() { $this->participants = new ArrayCollection(); } public function getParticipants() { return $this->participants; } public function setParticipants(ArrayCollection $participants) { $this->participants = $participants; } } 

Secondly, your member can be anything. For instance:

 class Participant { private $name; public function setName($name) { $this->name = $name; return $this; } public function getName() { return $this->name; } } 

Thirdly, your reservation type should contain a ComponentType collection, something like this:

 // ... $builder->add('participants', 'collection', array('type' => new ParticipantType())); 

Fourth, the type of participant is simple. According to my example:

 // ... $builder->add('name', 'text', array('required' => true)); 

Finally, in BookingController add the required number of Member to create the collection.

 // ... $entity = new Booking(); $participant1 = new Participant(); $participant1->name = 'participant1'; $entity->getParticipants()->add($participant1); // add entry to ArrayCollection $participant2 = new Participant(); $participant2->name = 'participant2'; $entity->getParticipants()->add($participant2); // add entry to ArrayCollection 
+7
source

I think you should add a type here:

  ->add('Participants', 'collection', array('type' => 'YourParticipantType')); 

Could you also insert the declaration of your addParticipant function from the model here? There seems to be something suspicious.

0
source

All Articles