根据Symfony docs,可以自定义单个收集表单类型。如何让Symfony检测我的自定义树枝模板? 似乎开箱即用,但是我可能错过了一些东西。
src \ Form \ Type \ ItemAliasType:
class ItemAliasType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("name", TextType::class, [
"label" => "item alias name",
"required" => true,
]);
$builder->add("description", TextType::class, [
"label" => "item alias name",
"required" => true,
]);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ItemAlias::class,
]);
}
}
src / Form / ItemForm:
class ItemForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
/* ... code ... */
$builder->add("aliases", CollectionType::class, array_merge([
"label" => "item-aliases",
"entry_type" => ItemAliasType::class,
"allow_add" => true,
"allow_delete" => true,
"prototype" => true,
], $inheritedOptions));
}
}
templates / form / collections.html.twig:
{% block _aliases_entry_row %}
<div class="custom">
{{ form_widget(form.name) }}
{{ form_widget(form.description) }}
</div>
{% endblock %}
config / packages / twig.yaml:
twig:
form_theme:
- 'form/collections.html.twig'
答案 0 :(得分:0)
树枝模板中的块应使用不同的名称
templates / form / collections.html.twig:
{% block _item_form_aliases_entry_row %}
要阻止的标识符应为<form_name><form_child_name>_entry_(row|widget|label)
Artamiels link提供了清晰的解释