简体   繁体   English

Symfony 3在EntityType表单字段中获取对象数据

[英]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.

相关问题 Symfony 5.4 - EntityType 多表单字段未选择数据中的选项 - Symfony 5.4 - EntityType multiple form field not selecting options in data Symfony:在 FormType 表单中为 EntityType 使用不同的字段 - Symfony: Use distinct field for EntityType in a FormType form 如果字段在对象类中具有Valid()约束,则具有EntityType字段的Symfony表单将生成“此值不应为空”错误 - Symfony form with EntityType field yields a 'This value should not be blank' error if the field has a Valid() contraint in the object class Symfony2:初始化EntityType字段为空数据 - Symfony2: init EntityType field with empty data Symfony表单验证EntityType - Symfony Form Validation EntityType Symfony 3 Form EntityType字段不包含空选项 - Symfony 3 Form EntityType field does not take empty choices 如何在 Symfony 5 中创建数量与 EntityType 字段相关联的表单? - How to create a form with quantities linked with an EntityType field in Symfony 5? 预填充 EntityType 类型的 Symfony 表单的字段 - Pre-filling a field of a Symfony form of type EntityType 如何从Symfony 2.8中的EntityType字段获取特定值 - How to get specific value from EntityType Field in Symfony 2.8 Symfony 5:如何在表单类型文件的实体类型字段中使用“数据”? - Symfony 5 : How to use 'data' in a entitytype field in a formtype file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM