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

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

问题描述

我想使用一种形式的几个实体的字段,我可以这样做吗?例如,我想从 ProfileType 类型和 name 中添加一个表单 surname / code>字段来自 CountryType 。这个字段必须是一个简单的字符串( text )。



我该怎么做?谢谢!



注意:我无法使用实体类型,因为Symfony仅提供复选框单选按钮选择,但是我需要使用简单的文本字段。

解决方案

从表单中的相关实体,您为每个相关实体嵌入自定义表单类型。理论上可以以单一形式显示和更新具有许多关系的复杂实体的所有数据。在实践中,这样做可以让许多关系变得复杂,但直接关系到一个关系。请参阅嵌入式表单:嵌入单个对象 Symfony表格文件。



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

  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,
));
}

然后在您的枝条中,您可以添加所需的字段: p>

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

假设您的ProfileType和CountryType将其他字段添加到表单中,那么如果使用form_rest(form)在你的树枝里,你会得到你不想要的其他字段,或者如果你不使用form_rest,那么根据你使用的symfony版本,你可能会收到错误。有多种方法来处理这个问题。



我有时候使用'form_widget(_token)'代替'form_rest(form)'来解决这个问题。不过,我不知道你将来可以依靠这种继续工作吗?您可以将form_rest(form)包装在隐藏的div中,因为它通常仅用于添加隐藏的_token字段,在这种情况下,您不想看到其他配置文件和国家/地区字段。在这种情况下,隐藏的实体值仍然映射到表单并返回,具有任何相应的开销,但是值不能被更改。



或者,您可以为个人资料和国家/地区实体提供多种表单类型,并为上下文使用适当的表单类型。我不知道你的表单是什么,但是,例如,您可能会在上面的buildForm()方法中使用EditThingProfileType和EditThingCountryType,每个都只添加窗体上需要的单个字段。


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).

How can I do it? Thanks!

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.

解决方案

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) }}

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.

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.

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天全站免登陆