Symfony Ajax 表单 - 选择类型选项取决于其他未映射的选择类型选项
Symfony Ajax Form - Choice Type Options depend on other unmapped Choice Type Options
我尽量保持简短。
我想根据另一个 ChoiceType(N.1) 更改我的 ChoiceType(N.2) 的可能选项。
- 我的 ChoiceType(N.1) 未映射并且选项为“是”或“否”
- 我的 ChoiceType(N.2) 已映射并填充了数据库中的类别。
数据库有 Columns ID(int) |名称(字符串) |
收入(布尔值)
因此,如果用户选择“是”选项,我希望 ChoiceType (N.2) 中的所有类别的收入为 1(true)。如果用户选择否,我想显示收入为 0 的类别的所有选项(布尔值)
我做了很多研究,但就是无法让它发挥作用。
我主要关注 de documentaion https://symfony.com/doc/current/form/dynamic_form_modification.html,但是 ChoiceTyps 都是实体,而不仅仅是一个。我几乎应该拥有它,但我不知道缺少什么。
以下是我的代码的重要部分:
报名表:
->add('choice', ChoiceType::class, [
'choices' => [
'No' => 0,
'Yes' => 1,
],
'data'=>0,
'mapped' => false,
])
$formModifier = function (FormInterface $form, $choice) {
$revenue = $choice;
$user = $this->security->getUser();
$userId = $user->getId();
$form->add('category', EntityType::class,[
'class'=>Category::class,
'query_builder' => function(CategoryRepository $repository) use($userId,
$revenue) {
$qb = $repository->createQueryBuilder('u');
return $qb
->where('u.user=' .$userId, 'u.revenue='. $revenue );
}, ]);
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
$data = $event->getData();
$form = $event->getForm();
$choice = $form->get('choice')->getData();
$formModifier($event->getForm(), $choice);
}
);
$builder->get('choice')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$choice = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $choice);
}
);
AJAX
var $choice = $('#entry_choice');
$choice.change(function() {
var $form = $(this).closest('form');
var data = {};
var choice = $('#entry_choice').val();
data= choice;
data[$choice] = 1;
$.ajax({
url: $form.attr('action'),
type: $form.attr('method'),
data : data,
complete: function(html) {
console.log( data);
$('#entry_category').replaceWith(
$(html.responseText).find('#entry_category'));
}
});
});
我想我没有正确理解从 ajax 传来的数据。
我试图实现以下目标:如果用户将 ChoiceType 从 No 更改为 Yes,则将值为 1 的变量 $choice 传递给表单。在 $formModifier 的表单中,它应该将变量 $revenue 更改为 $choice,这样我就可以使用我的 query_builder 过滤收入为 1 的所有类别。('u.revenue='.$revenue)
此致马格纳斯
而不是 data[$choice] = 1;
试试 data[$choice.attr('name')] = 1;
Symfony 期望数据以特定格式从前端发送。
如果您检查生成的 html 表单的来源,您将看到该格式。
我尽量保持简短。
我想根据另一个 ChoiceType(N.1) 更改我的 ChoiceType(N.2) 的可能选项。
- 我的 ChoiceType(N.1) 未映射并且选项为“是”或“否”
- 我的 ChoiceType(N.2) 已映射并填充了数据库中的类别。
数据库有 Columns ID(int) |名称(字符串) | 收入(布尔值)
因此,如果用户选择“是”选项,我希望 ChoiceType (N.2) 中的所有类别的收入为 1(true)。如果用户选择否,我想显示收入为 0 的类别的所有选项(布尔值)
我做了很多研究,但就是无法让它发挥作用。 我主要关注 de documentaion https://symfony.com/doc/current/form/dynamic_form_modification.html,但是 ChoiceTyps 都是实体,而不仅仅是一个。我几乎应该拥有它,但我不知道缺少什么。 以下是我的代码的重要部分:
报名表:
->add('choice', ChoiceType::class, [
'choices' => [
'No' => 0,
'Yes' => 1,
],
'data'=>0,
'mapped' => false,
])
$formModifier = function (FormInterface $form, $choice) {
$revenue = $choice;
$user = $this->security->getUser();
$userId = $user->getId();
$form->add('category', EntityType::class,[
'class'=>Category::class,
'query_builder' => function(CategoryRepository $repository) use($userId,
$revenue) {
$qb = $repository->createQueryBuilder('u');
return $qb
->where('u.user=' .$userId, 'u.revenue='. $revenue );
}, ]);
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
$data = $event->getData();
$form = $event->getForm();
$choice = $form->get('choice')->getData();
$formModifier($event->getForm(), $choice);
}
);
$builder->get('choice')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$choice = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $choice);
}
);
AJAX
var $choice = $('#entry_choice');
$choice.change(function() {
var $form = $(this).closest('form');
var data = {};
var choice = $('#entry_choice').val();
data= choice;
data[$choice] = 1;
$.ajax({
url: $form.attr('action'),
type: $form.attr('method'),
data : data,
complete: function(html) {
console.log( data);
$('#entry_category').replaceWith(
$(html.responseText).find('#entry_category'));
}
});
});
我想我没有正确理解从 ajax 传来的数据。 我试图实现以下目标:如果用户将 ChoiceType 从 No 更改为 Yes,则将值为 1 的变量 $choice 传递给表单。在 $formModifier 的表单中,它应该将变量 $revenue 更改为 $choice,这样我就可以使用我的 query_builder 过滤收入为 1 的所有类别。('u.revenue='.$revenue)
此致马格纳斯
而不是 data[$choice] = 1;
试试 data[$choice.attr('name')] = 1;
Symfony 期望数据以特定格式从前端发送。
如果您检查生成的 html 表单的来源,您将看到该格式。