javascript - 在 buildForm 中编辑子项
问题描述
我正在使用 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] 结果我想要一个未确定数量的按钮。按钮的最大数量由全局变量确定。 按钮 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' => ' '
]);
}
}
谢谢你。
解决方案
推荐阅读
- npm - 如何让 npm 使用来自本地 libsass-dev(由 apt 安装)的 sass 而不是下载自己的包?
- python-3.x - 使用 python 脚本安装程序
- php - MySQL 查询以防止嵌套树
- git - 替换图像或文本时的语义提交类型?
- javascript - 使用谷歌应用脚本返回多次出现的值
- docker - Jenkins Job 如何将 docker 映像提升到下一个环境
- python - 如何用 Beautifulsoup 处理嵌套的 html 表?
- css - 如何覆盖背景图像上的导航栏?
- c# - EF 与数据库上的字段失去连接
- asp.net - 为什么我在尝试通过 HTTPS 访问 apple-touch-icon.png 时收到错误 403,但在浏览器中进行测试时会出现 200?