EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.08k stars 1.02k forks source link

CollectionType or CollectionField don't work right #5207

Closed rfcdt closed 2 years ago

rfcdt commented 2 years ago

Describe the bug So, I've wanted to do a nested collection. And in page 'edit' I added CollectionField and to this field added custom Type (setEntryType()). In custom type I added CollectionType and put options [allow_add => true, allow_delete => true] And i tried to edit row. But i noticed that in frontend when create input, it added wrong id to this input.

To Reproduce The EasyAdmin version is 3.

1) Create crud controller and in method "configureFields" add CollectionField. To this field add setEntryType() and put custom type. 2) In the custom type in method "buildForm" add field with CollectionType and add options [allow_add => true] to look bug. 3) Open page with this form. Click F12 (to open an inspector). And click on "Add new item" and add items and one more time and one more time.

(OPTIONAL) Additional context This is my method "configureFields" of crud controller

  public function configureFields(string $pageName): iterable
      {
          yield IdField::new('id')->onlyOnIndex();

          if(Crud::PAGE_INDEX === $pageName) {
              yield TextField::new('name', 'Название');
              yield ArrayField::new('parent', 'Основная категория')
                  ->setFormTypeOptionIfNotSet('by_reference', false);
          }

          if(Crud::PAGE_EDIT === $pageName) {
              $id = $this->getContext()->getEntity()->getInstance()->getId();

              yield AssociationField::new('parent', 'Основная категория')
                  ->setQueryBuilder(function(QueryBuilder $queryBuilder) use($id) {
                      return $queryBuilder->andWhere('entity.id != :id')
                          ->setParameter('id', $id);
                  });
                  // ->setFormTypeOption('disabled','disabled');
              yield TextField::new('name', 'Название');
          }

          if(Crud::PAGE_NEW === $pageName) {
              yield AssociationField::new('parent', 'Основная категория')
                  ->setQueryBuilder(function(QueryBuilder $queryBuilder) {
                      return $queryBuilder->andWhere('entity.parent IS NULL');
                  });
              yield TextField::new('name', 'Название');
          }

          yield CollectionField::new('possibleParameters', 'Возможные значения')
                  ->setFormTypeOptionIfNotSet('by_reference', false)
                  ->setEntryIsComplex(false)
                  ->setEntryType(PossibleParameterType::class);
      }

And this is my custom type

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class)
        ->add('type', ChoiceType::class, [
            'choices' => [
                'Item' => 'item',
                'Filter' => 'filter'
            ]
        ])
        ->add('field', ChoiceType::class, [
            'choices' => [
                'Text Field' => 'text',
                'Select Field' => 'select',
                'Radio Field' => 'radio'
            ]
        ])
        ->add('possibleParameterValues', CollectionType::class, [
            'entry_type' => TextType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'label' => 'Values',
            // 'by_reference' => true
        ]);
}

I have entity Category that have a relation OneToMany to entity PossibleParameter that have a relation OneToMany to entity PossibleParameterValue

Screenshot from frontend with right values image

And screenshot with bug image

So, if you have questions ask me I have questions, how i fixed it? What method do I need to rewrite? Can you help me?

rfcdt commented 2 years ago

Sorry guys. I've found how to solve this problem. Need just add it custom type in field CollectionType a option "prototype_name". I wasted all day to solve this problem... ohhh