please tell me, integrated the edit function from the Form Collections documentation from ( http://symfony.com/doc/current/form/form_collections.html )

There is an Entity (Ability) with a set of fields that need to be edited en masse, a bunch of imputs and one "save" button, in essence all getters and setters are written. , there are no problems with this, I started doing it according to the example from the documentation, I did it in the Entity folder

AbilityHolder.php

<?php namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="AbilityHolder") */ class AbilityHolder { protected $abilitys; public function __construct() { $this->abilitys = new ArrayCollection(); } public function getAbilitys() { return $this->abilitys; } } 

to him did

AbilityHolderType.php

 <?php namespace App\Form; use App\Entity\AbilityHolder; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Form\Extension\Core\Type\CollectionType; class AbilityHolderType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('abilitys', CollectionType::class, array( 'entry_type' => AbilityType::class, 'entry_options' => array('label' => false), 'allow_add' => true, )) ; } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'data_class' => AbilityHolder::class, ]); } } 

I registered a template in the tweek file from the example (everything works for a new record)

 {# ... #} {{ form_start(form) }} {# render the task's only field: description #} <ul class="abilitys"> {# iterate over each existing tag and render its only field: name #} {% for ability in form.abilitys %} <li>{{ form_row(ability.idItem) }}</li> <li>{{ form_row(ability.code) }}</li> <li>{{ form_row(ability.reloadTime) }}</li> <li>{{ form_row(ability.durationTime) }}</li> <li>{{ form_row(ability.idAbilityType) }}</li> {% endfor %} </ul> {{ form_end(form) }} {# ... #} 

and the controller itself with the function New and Edit

 <?php namespace App\Controller; use App\Entity\AbilityHolder; use App\Entity\Ability; use App\Form\AbilityHolderType; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Routing\Annotation\Route; use Doctrine\Common\Collections\ArrayCollection; use App\Controller\EntityManagerInterface; /** * @Route("/ability-holder") */ class AbilityHolderController extends Controller { /** * @Route("/new", name="ability_new", methods="GET|POST") */ public function new(Request $request) { $abilitys = new AbilityHolder(); $ability1 = new Ability(); $ability1->setIdItem('1'); $ability1->setCode('code'); $ability1->setReloadTime('1'); $ability1->setDurationTime('1'); $ability1->setIdAbilityType('1'); // dummy code - this is here just so that the Ability has some tags // otherwise, this isn't an interesting example $abilitys->getAbilitys()->add($ability1); // end dummy code $form = $this->createForm(AbilityHolderType::class, $abilitys); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // ... maybe do some form processing, like saving the Ability and Tag objects } return $this->render('ability/new-holder.html.twig', array( 'form' => $form->createView(), )); } /** * @Route("/edit", name="ability_edit", methods="GET|POST") */ public function edit($id, Request $request) { $entityManager = $this->getDoctrine()->getManager(); if (null === $abilityHolder = $entityManager->getRepository(AbilityHolder::class)->find($id)) { throw $this->createNotFoundException('No task found for id '.$id); } $originalAbilitys = new ArrayCollection(); $abilityHolder = new AbilityHolder(); // Create an ArrayCollection of the current Tag objects in the database foreach ($abilityHolder->getAbilitys() as $ability) { $originalAbilitys->add($ability); } $editForm = $this->createForm(AbilityHolderType::class, $abilityHolder); $editForm->handleRequest($request); if ($editForm->isSubmitted() && $editForm->isValid()) { // remove the relationship between the ability and the Task foreach ($originalAbilitys as $ability) { if (false === $abilityHolder->getAbilitys()->contains($ability)) { // remove the Task from the Tag $ability->getAbilitys()->removeElement($abilityHolder); // if it was a many-to-one relationship, remove the relationship like this // $ability->setTask(null); $entityManager->persist($ability); // if you wanted to delete the Tag entirely, you can also do that // $entityManager->remove($ability); } } $entityManager->persist($abilityHolder); $entityManager->flush(); // redirect back to some edit page return $this->redirectToRoute('ability_edit', array('id' => $id)); } return $this->render('ability/new-holder.html.twig', array( 'form' => $editForm->createView(), )); // render some form template } } 

already completely confused, I probably missed something, deliberately did not read about the add and remove functions because there jquery

displays now just <label class="required">Abilitys</label>

but if you open the web inspector and see what the page consists of, then there will be the necessary data, but they will not be visible, no styles will block them enter image description here

Maybe I'm not picking at all?

  • The route must be / edit / {id}, otherwise what entity are you editing? - u_mulder
  • yes, I really didn’t remove it from the example, and I’ve removed the check below, now it’s displaying a blank page, but if you open F12 (I don’t remember how the developer’s mode is called) there is a div in which you ’ve got the necessary entries, updated the theme there are screenshots
  • There are no items in the AbiliteHolder. You are presented with a template for a new item to add. - u_mulder
  • In the symphony manual, everything is described with how to work with a collection of fields. - u_mulder
  • in the documentation only about edit and delete, in principle they do not describe how to create such a page so that everything is displayed in input and saved at once, apparently the Collections will not work for this? - Evgeny Melentev

0