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

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

问题描述

我有一个在Symfony中构建的表单,并且在视图中呈现时,html表单可能包含或不包含表单对象中的所有字段(实体类型有几个不同的状态,并非所有字段

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

有没有什么办法可以告诉Symfony不要那么愚蠢,只会处理或者是否必须在handleRequest调用之前复制实体,然后遍历POST值并复制帖子中的相关值-handleRequest实体转换为实体的pre-handleRequest克隆,因此我保留了不在POST数据中的字段。



phew!正如你所看到的那样,它有点愚蠢的解决方案,有点愚蠢的问题,tbh。



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



感谢您的帮助。



问候



Steve。

解决方案

'使用 handleRequest



您应该使用 submit 直接代替 clearMissing 参数设置为false。


$ b

Symfony / Component / Form / FormInterface

  / ** 
*将数据提交到窗体,转换并验证它。
*
* @param null | string | array $ submittedData提交的数据。
* @param bool $ clearMissing当
*提交的数据中缺少字段时,是否将字段设置为NULL
*。
*
* @return FormInterface表单实例
*
* @throws Exception\AlreadySubmittedException如果表单已经提交。
* /
public function submit($ submittedData,$ clearMissing = true);

当您使用 handleRequest 时,你需要提交的数据,然后使用 $ form-> submit($ data,'PATCH'!== $ method); 提交它,这意味着除非你有使用 PATCH 方法提交表单,然后它将清除字段。



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

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

..它从请求中获取表单数据数组并直接提交它,但是明显缺失字段参数设置为false。


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

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.

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

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.

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

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.

Thanks for any help.

Regards

Steve.

解决方案

In short, don't use handleRequest.

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 Exception\AlreadySubmittedException If the form has already been submitted.
 */
public function submit($submittedData, $clearMissing = true);

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

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