symfony:我们不能有一个隐藏的实体字段吗? [英] symfony : can't we have a hidden entity field?

查看:111
本文介绍了symfony:我们不能有一个隐藏的实体字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用symfony渲染一个带有实体字段的表单.

I am rendering a form with an entity field in symfony.

当我选择常规实体字段时,效果很好.

It works well when i choose a regular entity field.

$builder
    ->add('parent','entity',array(
            'class' => 'AppBundle:FoodAnalytics\Recipe',
            'attr' => array(
                'class' => 'hidden'
            )
        ))

当我选择-> add('parent','hidden')时,它会引发以下错误:

It throws the following error when I choose ->add('parent','hidden') :

该表单的视图数据应为标量,数组或 \ ArrayAccess的实例,但是类的实例 AppBundle \ Entity \ FoodAnalytics \ Recipe.您可以通过以下方式避免此错误 将"data_class"选项设置为 "AppBundle \ Entity \ FoodAnalytics \ Recipe"或通过添加视图 转换类实例的转换器 AppBundle \ Entity \ FoodAnalytics \ Recipe为标量,数组或实例 \ ArrayAccess. 500内部服务器错误-LogicException

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class AppBundle\Entity\FoodAnalytics\Recipe. You can avoid this error by setting the "data_class" option to "AppBundle\Entity\FoodAnalytics\Recipe" or by adding a view transformer that transforms an instance of class AppBundle\Entity\FoodAnalytics\Recipe to scalar, array or an instance of \ArrayAccess. 500 Internal Server Error - LogicException

我们不能拥有隐藏的实体字段吗?为什么不?我是否必须放置另一个隐藏字段来检索实体ID?

Can't we have hidden entity fields ?? Why not? Am I obliged to put another hidden field to retrieve the entity id?

基本上,我想做的是在显示表单之前先对其进行水化处理,但阻止用户更改其字段之一(此处为父级). 这是因为我需要将Id作为参数传递,而不能在表单操作url中传递它.

Basically, what I'm trying to do is to hydrate the form before displaying it but prevent the user to change one of its fields (the parent here). This is because I need to pass the Id as a parameter and I can't do it in the form action url.

推荐答案

我认为您对字段类型及其各自代表的含义感到困惑.

I think you are simply confused about the field types and what they each represent.

entity字段是choice字段的一种.选择字段旨在包含用户可以在表格中选择的值.呈现此表单时,Symfony将基于实体字段的基础类生成一个可能选择的列表,并且列表中每个选择的值是相应实体的ID.提交表单后,Symfony将为您水合一个代表所选实体的对象. entity字段通常用于呈现实体关联(例如,您可以选择分配给userroles列表).

An entity field is a type of choice field. Choice fields are meant to contain values selectable by a user in a form. When this form is rendered, Symfony will generate a list of possible choices based on the underlying class of the entity field, and the value of each choice in the list is the id of the respective entity. Once the form is submitted, Symfony will hydrate an object for you representing the selected entity. The entity field is typically used for rendering entity associations (like for example a list of roles you can select to assign to a user).

如果只是尝试为实体的ID字段创建占位符,则可以使用hidden输入.但这仅在您要创建的表单类表示一个实体时才有效(即,表单的data_class引用您已定义的实体).然后,ID字段将正确映射到由表单的data_class定义的类型的实体的ID.

If you are simply trying to create a placeholder for an ID field of an entity, then you would use the hidden input. But this only works if the form class you are creating represents an entity (ie the form's data_class refers to an entity you have defined). The ID field will then properly map to the ID of an entity of the type defined by the form's data_class.

下文描述的解决您特定情况的一种解决方案是创建一个新的字段类型(我们称它为EntityHidden),该字段类型扩展了hidden字段类型,但处理数据转换以转换为实体/id.这样,您的表单将包含实体ID作为隐藏字段,但是一旦提交表单,应用程序将可以访问实体本身.当然,转换是由数据转换器执行的.

One solution to your particular situation described below would be to create a new field type (let's call it EntityHidden) that extends the hidden field type but handles data transformation for converting to/from an entity/id. In this way, your form will contain the entity ID as a hidden field, but the application will have access to the entity itself once the form is submitted. The conversion, of course, is performed by the data transformer.

以下是此类实现的一个示例,以供后代使用:

Here is an example of such an implementation, for posterity:

namespace My\Bundle\Form\Extension\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\DataTransformerInterface;

/**
 * Entity hidden custom type class definition
 */
class EntityHiddenType extends AbstractType
{
    /**
     * @var DataTransformerInterface $transformer
     */
     private $transformer;

    /**
     * Constructor
     *
     * @param DataTransformerInterface $transformer
     */
    public function __construct(DataTransformerInterface $transformer)
    {
        $this->transformer = $transformer;
    }

    /**
     * @inheritDoc
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // attach the specified model transformer for this entity list field
        // this will convert data between object and string formats
        $builder->addModelTransformer($this->transformer);
    }

    /**
     * @inheritDoc
     */
    public function getParent()
    {
        return 'hidden';
    }

    /**
     * @inheritDoc
     */
    public function getName()
    {
        return 'entityhidden';
    }
}

请注意,在表单类型类中,您要做的就是将隐藏实体分配给其对应的表单字段属性(在表单模型/数据类中),Symfony会正确生成ID为的隐藏输入HTML.实体作为其价值.希望有帮助.

Just note that in the form type class, all you have to do is assign your hidden entity to its corresponding form field property (within the form model/data class) and Symfony will generate the hidden input HTML properly with the ID of the entity as its value. Hope that helps.

这篇关于symfony:我们不能有一个隐藏的实体字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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