[英]Symfony 3 get object data in EntityType form field
I've got the Form with two EntityType fields 我有两个EntityType字段的窗体
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('supplier', EntityType::class, array(
'class' => 'AppBundle:Supplier',
'choice_label' => 'pubName',
'label' => false,
'expanded' => true
))
->add('payment', EntityType::class, array(
'class' => 'AppBundle:Payment',
'choice_label' => 'pubName',
'label' => false,
'expanded' => true
));
}
And I want to get fields of passed objects in Twig template, so I could get the name, or description of every entity to customize my form view. 而且我想在Twig模板中获取传递对象的字段,因此我可以获取名称或每个实体的描述以自定义表单视图。 How can I achieve that in the way?
我怎样才能做到这一点?
You can create a custom type with EntityType as parent for both of your entities. 您可以为两个实体创建一个以EntityType作为父项的自定义类型。
Take a look at this documentation page: https://symfony.com/doc/3.4/form/create_form_type_extension.html 看看这个文档页面: https : //symfony.com/doc/3.4/form/create_form_type_extension.html
You can access the current data of your form via form.vars.value
: 您可以通过
form.vars.value
访问表单的当前数据:
{{ form.vars.value.supplier }} {# Your Supplier object #}
{{ form.vars.value.payment }} {# Your Payment object #}
Reference: How to Control the Rendering of a Form 参考: 如何控制表单的呈现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.