我正在设置一个带有Collection元素的表单。我按照the docs
所述设置了原型我已经设置了JavaScript以管理动态添加/删除部分。
但是我想添加已保存元素的实体id作为数据元素id,因为我想对已经保存的集合元素执行ajax检查(在表单填充期间,而不是在提交阶段),如果有的话现场变化。因此,我需要将实体ID赋予DOm,以传递给数据。
<form ...>
<ul class="tariffAdjustments ulprototype" data-prototype="{{ protos.tariffAdjustment_prototype(form.tariffAdjustments.vars.prototype)|e('html_attr') }}">
{% for tariffAdjustment in form.tariffAdjustments %}
<li class="eltariffadjustment" data-taid="XXX">
{# can't retrieve the element id #}{{dump(form.tariffAdjustments.vars)}}
{{ protos.tariffAdjustment_prototype(tariffAdjustment)|raw }}
</li>
{% endfor %}
</ul>
</form>
在{{dump(form.tariffAdjustments.vars)}}
中,我有:
FormView {#18167
+vars: array:30 [
"value" => PersistentCollection {#9714}
"form" => FormView {#18167}
"id" => "module_entity_tariffAdjustments"
...
"prototype" => FormView {#18189 ▼
+vars: array:27 [▼
"id" => "module_entity_tariffAdjustments___name__"
...
]
+parent: FormView {#18167}
+children: array:10 []
"name" => FormView {#18192} <= entity field exposed in the FormType
"startDate" => FormView {#18197}
...
-rendered: false
}
]
+parent: FormView {#8546 ▶}
+children: array:3 [▼
0 => FormView {#18336 ▼
+vars: array:27 [▼
"value" => TariffAdjustment {#9798 ▼
-id: 154 <= what i'm looking for
...
}
...
]
+parent: FormView {#18167}
+children: array:10 []
-rendered: false
}
1 => FormView {#18338 ▼
+vars: array:27 [▼
"value" => TariffAdjustment {#9798 ▼
-id: 172 <= what i'm looking for
...
}
...
]
+parent: FormView {#18167}
+children: array:10 []
-rendered: false
}
]
-rendered: false
}
我的实体ID显然不在我的FormType中:我不想设置它。
主要形式
class ModuleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$em = $options['entity_manager'];
$builder
->add('tariffAdjustments', CollectionType::class, [
'label' => 'module.tariffAdjustments',
'required' => false,
'attr' => [
'class' => 'collectiondata',
],
'constraints' => new Valid(),
'entry_type' => TariffAdjustmentType::class,
'entry_options' => ['entity_manager' => $em],
'by_reference' => false,
'allow_add' => true,
'allow_delete' => true,
]);
}
}
以及收集表格
class TariffAdjustmentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$em = $options['entity_manager'];
$builder
->add('name', TextType::class, [
'label' => 'name',
'label_attr' => ['class' => 'mandatory'],
'required' => true,
]);
...
}
}
也许我错过了一些明显的事情,但是我找不到。 谢谢
答案 0 :(得分:0)
我多么愚蠢:
我正在设置{{dump(form.tariffAdjustments.vars)}}
但由于我处于循环状态,因此只需要设置:tariffAdjustment.vars.value.id
,这是我要查找的实体的ID。