gpt4 book ai didi

forms - Symfony2 动态/依赖形式

转载 作者:行者123 更新时间:2023-12-01 01:24:23 25 4
gpt4 key购买 nike

我有一个包含 3 个依赖字段的表单:

制造商 -> 制造商产品组 -> 制造商产品系列

所以我想选择一个制造商,基于制造商的产品组和基于产品组的产品系列。

有一个关于如何处理这种动态表单的 CookBook Entry
它很容易为 ManufacturerProductGroup 工作。问题是
ManufacturerProductSeries 依赖于另一个动态表单/字段。

问题是,我不能在闭包中添加一个监听器,因为我只有
对 FormInterface 的访问不再是对 FormBuilderInterface 的访问。

我不能像 PRE_SET_DATA Listener 那样只用一个监听器来完成,因为表单
将提交然后防止任何修改。我要么需要一种方法来添加
动态监听器或者可能只更新 POST_SUBMIT 监听器中的数据
而不是替换整个字段/表单。

这是由于我向不存在的监听器添加监听器而导致错误的代码
表单字段(manufacturerProductGroup 仅在 pre_set_data 事件之后可用)

/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('manufacturer', 'entity', array('class' => 'ArticleBundle:manufacturer', 'empty_value' => false));

$manufacturerProductGroupModifier = function(FormInterface $form, Manufacturer $manufacturer)
{
$manufacturerProductGroups = $this->em->getRepository('ArticleBundle:ManufacturerProductGroup')
->findAvailableByManufacturer($manufacturer);

$form->add(
'manufacturerProductGroup',
'entity',
array(
'class' => 'ArticleBundle:ManufacturerProductGroup',
'empty_value' => '',
'choices' => $manufacturerProductGroups
)
);
};

$manufacturerProductSeriesModifier = function(FormInterface $form, ManufacturerProductGroup $manufacturerProductGroup)
{
$manufacturerProductSeries = $this->em->getRepository('ArticleBundle:ManufacturerProductSeries')
->findAvailableByManufacturerProductGroup($manufacturerProductGroup);

$form->add(
'manufacturerProductSeries',
'entity',
array(
'class' => 'ArticleBundle:ManufacturerProductSeries',
'empty_value' => '',
'choices' => $manufacturerProductSeries
)
);

};

$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event) use ($manufacturerProductGroupModifier, $manufacturerProductSeriesModifier) {
/** @var $article Article */
$article = $event->getData();
$manufacturerProductGroupModifier($event->getForm(), $article->getManufacturer());
$manufacturerProductSeriesModifier($event->getForm(), $article->getManufacturerProductGroup());
}
);

$builder->get('manufacturer')->addEventListener(
FormEvents::POST_SUBMIT,
function(FormEvent $event) use ($manufacturerProductGroupModifier) {
$manufacturer = $event->getForm()->getData();
$manufacturerProductGroupModifier($event->getForm()->getParent(), $manufacturer);
}
);

$builder->get('manufacturerProductGroup')->addEventListener(
FormEvents::POST_SUBMIT,
function(FormEvent $event) use ($manufacturerProductSeriesModifier) {
$manufacturerProductGroup = $event->getForm()->getData();
$manufacturerProductSeriesModifier($event->getForm()->getParent(), $manufacturerProductGroup);
}
);
}

最佳答案

我用以下代码解决了这个问题:

    $addManufacturerProductSeries = function (FormEvent $event) {
$form = $event->getForm()->getParent();
$manufacturerProductGroup = $event->getData();

$form->add(
'manufacturerProductSeries',
'entity',
array(
'auto_initialize' => false,
'class' => 'ArticleBundle:ManufacturerProductSeries',
'query_builder' => function (ManufacturerProductSeriesRepository $repository) use ($manufacturerProductGroup) {
return $repository->findAvailableByManufacturerProductGroupQueryBuilder($manufacturerProductGroup);
},
)
);
};

$addManufacturerProductGroup = function (FormEvent $event) use ($factory, $addManufacturerProductSeries) {
$form = $event->getForm()->getParent();
$manufacturer = $event->getData();

$fieldBuilder = $factory->createNamedBuilder(
'manufacturerProductGroup',
'entity',
null,
array(
'auto_initialize' => false,
'class' => 'ArticleBundle:ManufacturerProductGroup',
'query_builder' => function (ManufacturerProductGroupRepository $repository) use ($manufacturer) {
return $repository->findAvailableByManufacturerQueryBuilder($manufacturer);
},
)
);

$fieldBuilder->addEventListener(FormEvents::POST_SET_DATA, $addManufacturerProductSeries);
$fieldBuilder->addEventListener(FormEvents::POST_SUBMIT, $addManufacturerProductSeries);

$form->add($fieldBuilder->getForm());
};

$builder->get('manufacturer')->addEventListener(FormEvents::POST_SET_DATA, $addManufacturerProductGroup);
$builder->get('manufacturer')->addEventListener(FormEvents::POST_SUBMIT, $addManufacturerProductGroup);

关于forms - Symfony2 动态/依赖形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19904215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com