简体   繁体   English

Symfony2从提交的表单数据中删除ID字段

[英]Symfony2 remove id field from submitted form data

My Symfony2 form has a text field containing the entity's ID. 我的Symfony2表单有一个包含实体ID的文本字段。

I used the following to achieve this: 我使用以下方法实现了这一目标:

$formBuilder->add('id', TextType::class, ['attr' => ['readonly' => true]])

When submitting the form, which represents an existing entity, the ID is passed and Doctrine searches for an accessor method to modify the ID. 提交代表现有实体的表单时,将传递ID,Doctrine会搜索用于修改ID的访问器方法。 At this point, an exception is thrown, as the entity doesn't allow to change the ID. 此时,由于实体不允许更改ID,因此引发了异常。

What is the common way in Symfony2 to provide a read-only field, which is not meant to be saved? Symfony2中提供只读字段(不保存该字段)的常用方法是什么?

readonly is for clients. readonly适用于客户端。 It indicates that client can't change the value of the element. 它指示客户端无法更改元素的值。 But it is going to be submitted with form. 但是它将与表单一起提交。

If you don't want element's value to be submitted use disabled instead. 如果您不希望提交元素的值,请使用disabled

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM