Symfony2 - 如何阻止 Form->handleRequest 将发布数据中不存在的字段置空 [英] Symfony2 - How to stop Form->handleRequest from nulling fields that don't exist in post data

查看:19
本文介绍了Symfony2 - 如何阻止 Form->handleRequest 将发布数据中不存在的字段置空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 Symfony 中构建的表单,当在视图中呈现时,html 表单可能包含也可能不包含表单对象中的所有字段(实体类型有几个不同的状态而不是所有字段在视图中呈现).

I've got a form built in Symfony and when rendered in the view, the html form may or may not contain all of the fields in the form object (the entity sort of has a couple of different states and not all fields are rendedered in the view).

问题在于,当表单在提交处理程序中处理时,通过表单对象的 handleRequest() 方法,它将实体中不存在于 post 数据中的任何属性重置为 null,吹走任何现有值.

The problem is that when the form is processed in the submit handler, via handleRequest() method of the form object, it resets any properties in the entity that are not present in the post data to null, blowing away any existing value.

有什么方法可以告诉 Symfony 不要那么笨,只处理 POST 数据中存在的字段?

Is there any way to tell Symfony not to be so stupid and only process the fields present in the POST data?

或者我是否必须在 handleRequest 调用之前克隆实体,然后遍历 POST 值并将相关值从 post-handleRequest 实体复制到实体的 pre-handleRequest 克隆,所以我保留了那些字段不在 POST 数据中.

Or do I have to clone the entity before the handleRequest call and then loop over the POST values and copy the related values from the post-handleRequest entity over to the pre-handleRequest clone of the entity, so I preserve the fields that are not in the POST data.

呸!正如你所看到的,它有点愚蠢的解决方案,有点愚蠢的问题,tbh.

phew! as you can see, its a bit of a daft solution, to a bit of a daft problem, tbh.

如果实体实际上是一个新创建的对象,我可以理解 symfony 这样做,但是它是从数据库加载的,然后调用了 handleRequest - 知道该对象已经初始化并且只设置字段应该足够明智传入POST数据.

I could understand symfony doing this if the entity was in effect a newly created object, but its been loaded from the DB and then handleRequest called - it should be sensible enough to know the object has already been initialised and only set the fields passed in the POST data.

感谢您的帮助.

问候

史蒂夫.

推荐答案

总之,不要使用handleRequest.

您应该直接使用 submit 以及将 clearMissing 参数设置为 false.

You should use submit directly instead along with the clearMissing parameter set to false.

Symfony/Component/Form/FormInterface

/**
 * Submits data to the form, transforms and validates it.
 *
 * @param null|string|array $submittedData The submitted data.
 * @param bool              $clearMissing  Whether to set fields to NULL
 *                                         when they are missing in the
 *                                         submitted data.
 *
 * @return FormInterface The form instance
 *
 * @throws ExceptionAlreadySubmittedException If the form has already been submitted.
 */
public function submit($submittedData, $clearMissing = true);

当您使用 handleRequest 时,它会计算出您想要提交的数据,然后使用 $form->submit($data, 'PATCH' !== $method);,这意味着除非您使用 PATCH 方法提交表单,否则它将清除字段.

When you use handleRequest it works out what data you are wanting the submit and then submits it using $form->submit($data, 'PATCH' !== $method);, meaning that unless you have submitted the form using the PATCH method then it will clear the fields.

要自己提交表单而不清除您的可以使用...

To submit the form yourself without clearing your can use...

$form->submit($request->get($form->getName()), false);

.. 从请求中获取表单数据数组并直接提交,但将 clear missing fields 参数设置为 false.

.. which get the form data array from the request and submit it directly, but with the clear missing fields parameter set to false.

这篇关于Symfony2 - 如何阻止 Form->handleRequest 将发布数据中不存在的字段置空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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