如何在实体表单类型中包含来自另一个实体的某些字段? [英] How to include in entity form type some fields from another entity?

查看:21
本文介绍了如何在实体表单类型中包含来自另一个实体的某些字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以一种形式使用来自少数实体的字段,我可以这样做吗?例如,我想将 ProfileType 类型中的 surname 字段和 CountryType 中的 name 字段添加到一个表单中.此字段必须是一个简单的字符串 (text).

I want to use fields from few entities in one form, can I do it? For example, I want to add to one form surname field from ProfileType type and name field from CountryType. This fields must be a simple string (text).

我该怎么做?谢谢!

注意:我不能使用 实体类型,因为Symfony只提供复选框单选按钮选择,但是我需要使用一个简单的text 字段.

NOTE: I can't use entity type, becouse Symfony provide only checkboxes, radio buttons and selects for it, however I need to use a simple text field.

推荐答案

要在表单中包含来自相关实体的字段,您可以为每个相关实体嵌入自定义表单类型.从理论上讲,可以在单个表单中显示和更新具有许多关系的复杂实体的所有数据.实际上,对 toMany 关系执行此操作可能会变得复杂,但对 toOne 关系执行此操作很简单.请参阅嵌入式表单:嵌入单个对象Symfony 表单文档.

To include fields from related entities in a form you embed a custom form type for each related entity. Theoretically it is possible to display and update all the data for a complex entity with many relationships in a single form. In practice, doing this for toMany relationships can get complicated but it is straightforward for toOne relationships. See Embedded Forms: Embedding a single object in the Symfony Forms documentation.

例如,在您的主要实体的表单类型中:

For example, in the form type for your main entity:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    ...
    $builder->add('profile', new ProfileType());
    $builder->add('country', new CountryType());
    ...
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        ...,
        'cascade_validation' => true,
    ));
}

然后在你的树枝中,你可以像这样添加你需要的字段:

Then in your twig you can add the fields you require like this:

{{ form_widget(form.profile.surname) }}
{{ form_widget(form.country.name) }}

假设您的 ProfileType 和 CountryType 将其他字段添加到它们的表单中,那么如果您在树枝中使用form_rest(form)",您将获得您不想要的其他字段,或者如果您不使用 form_rest,则取决于您使用的 symfony 版本可能会出错.处理这个问题的方法不止一种.

Assuming your ProfileType and CountryType add other fields to their forms then if you use "form_rest(form)" in your twig you will get the other fields that you don't want, or if you don't use form_rest then depending which symfony version you are using you might get errors. There is more than one way to handle this.

我有时使用form_widget(_token)"代替form_rest(form)"来解决这个问题.但是,我不知道您是否可以依靠它在未来继续工作.您可以将form_rest(form)"包装在隐藏的 div 中,因为它通常仅用于添加隐藏的_token"字段,在这种情况下,您不想看到其他 Profile 和 Country 字段.在这种情况下,隐藏实体值仍然映射到表单并返回,并带有任何相应的开销,但这些值不能更改.

I sometimes use 'form_widget(_token)' in place of 'form_rest(form)' to get round this issue. However I don't know if you can rely on this continuing to work in the future. You could wrap "form_rest(form)" in a hidden div as it is usually only used to add the hidden '_token' field and in this case you don't want to see the other Profile and Country fields. In this case the hidden entity values are still mapped to the form and back, with any corresponding overhead, but the values cannot be changed.

或者,您可以为 Profile 和 Country 实体使用多种表单类型,并为上下文使用适当的一种.我不知道您的表单是做什么用的,但例如,您可能在上面的 buildForm() 方法中使用了 EditThingProfileType 和 EditThingCountryType,每个都只在表单上添加了您需要的单个字段.

Alternatively, you can have multiple form types for your Profile and Country entities and use the appropriate one for the context. I don't know what your form is for but, for example, you might have EditThingProfileType and EditThingCountryType for use in the buildForm() method above, each adding only the single field you require on your form.

这篇关于如何在实体表单类型中包含来自另一个实体的某些字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆