在buildForm中编辑子项

时间:2018-08-27 09:07:28

标签: javascript php symfony symfony-3.4

我正在使用Symfony 3.4.7。我在表格上工作。 我有一个带有函数buildForm的formType和一些孩子。 我将eventListener添加到几个孩子。 在附加到eventListener的函数中,我想编辑一个存在的子级。但是我想编辑一个也附加了eventListener的孩子。

这是我的表格:

<?php

namespace AppBundle\Form;
// use ...

class ArticlesCategoriesType extends AbstractType
{
private $em;
private $ac;
private $max_long_arbo;

public function __construct(EntityManagerInterface $em, ArborescenceCategories $ac, $max_prof_arbo_cat)
{
    $this->em = $em;
    $this->ac = $ac;
    $this->max_long_arbo = $max_prof_arbo_cat;
}

/**
 * Construction du formulaire
 *
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $parents = $this->em->getRepository('AppBundle:Categories')->findByParent(null);
    $builder

        ->add('categorie_enfant1', EntityType::class, [
            'class' => 'AppBundle\Entity\Categories',
            'placeholder' => 'Sélectionnez la gamme',
            'choices' => $parents,
            'mapped' => false,
            'label' => ' '
        ])
        ->add('codeCategorie', HiddenType::class, [
            'label' => ' '
        ]);


        $builder->add('categorie_enfant2', HiddenType::class, [
            'mapped' => false,
            'label' => ' '
        ]);

    $builder->get('categorie_enfant1')->addEventListener(
        FormEvents::POST_SUBMIT,
        function (FormEvent $event){
            $form = $event->getForm();
            /* @var $categorie Categories */
            $categorie = $form->getData();

            $this->addEnfantCategorie($event->getForm(), 2, $categorie);
        }
    );

    $builder->get('categorie_enfant2')->addEventListener(
        FormEvents::POST_SUBMIT,
        function (FormEvent $event){
            $form = $event->getForm();
             /* @var $categorie Categories */
             $categorie = $form->getData();
             $this->addEnfantCategorie($form, 3, $categorie);
        }
    );
 }
 }

以及函数addEnfantCategorie的代码:

<?php
public function addEnfantCategorie(FormInterface $form, $i, Categories $categorie){
    $builder = $form->getParent();
    // récupère les catégories enfants
    $categories = null;
    dump($form->getName().' '.$categorie);
    $categories = $this->em->getRepository('AppBundle:Categories')->findByParent($categorie->getIdCategorie());
    $builder->add('categorie_enfant2', EntityType::class, [
            'class' => 'AppBundle\Entity\Categories',
            'label'=> ' ',
            'placeholder' => 'Sélectionner la catégorie',
            'choices' => $categories,
            'mapped' => false
        ]);
}

要编辑一个孩子,我找到了一种解决方案,以添加与我在“来源”构建器中添加的孩子同名的孩子。但它会删除附加到它的eventListener。

我没有找到仅编辑子项而不擦除具有相同名称的子项的解决方案。 我希望有一个现有的解决方案。

[编辑1] 结果,我希望按钮的数量不确定。按钮的最大数量由全局变量确定。 enter image description here 按钮1仅显示没有父母的类别。当我在按钮2上选择类别时,我希望它显示类别,这些类别是按钮其余部分的类别1,等等。这就是为什么我需要所有按钮的eventListener形式。为了动态显示,我使用Javascript。

[编辑2] 我尝试过使用嵌入表格。 这是嵌入表单的代码:

<?php

namespace AppBundle\Form;

use ...

class CategoryType extends AbstractType{

private $em;

public function __construct(EntityManagerInterface $em)
{
    $this->em = $em;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $parents = $this->em->getRepository('AppBundle:Categories')->findByParent(null);
    $builder->add('reference', EntityType::class, [
        'class' => 'AppBundle\Entity\Categories',
        'placeholder' => 'Sélectionner la gamme',
        'choices' => $parents,
        'label' => ' '
    ]);
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => Categories::class,
    ));
}
}

我将以CatgeoryType的形式传递属性,该属性是父级。我想使用此属性“父母”来获得这个父母的所有孩子。但是我不知道该怎么办。

在我的主要形式ArticlesCatgeories中,当我在构建器中添加CatgeoryType时,我想发送参数“ parent”,这是由先例“ categorie_enfant”选择的类别。

这是我的主要表格ArticlesCategoriesType的代码:

<?php

namespace AppBundle\Form;

use ...

class ArticlesCategoriesType extends AbstractType
{
private $em;
private $ac;
private $max_long_arbo;

public function __construct(EntityManagerInterface $em, ArborescenceCategories $ac, $max_prof_arbo_cat)
{
    $this->em = $em;
    $this->ac = $ac;
    $this->max_long_arbo = $max_prof_arbo_cat;

}

/**
 * Construction du formulaire
 *
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $enfants = $this->ac->getDernierEnfants($this->em);
    $builder
        ->add('codeCategorie', null, [
            'placeholder' => 'Sélectionnez une catégorie',
            'choices' => $enfants
        ])
        // ... others attributes 
    ->add('categorie_enfant1', CategoryType::class, [
        'parent' => 'A1'
    ])
    ->add('categorie_enfant2', CategoryType::class, [
            'mapped' => false,
            'label' => ' '
        ]);
     }
   }

谢谢。

0 个答案:

没有答案