Symfony 2是否支持部分形式绑定? [英] Does Symfony 2 support partial form binding?

查看:69
本文介绍了Symfony 2是否支持部分形式绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题

  1. Symfony 2是否支持PATCH请求或任何类型的 partial 表单内容提交?如果是这样,是否有一种正确"(或更准确地说是首选")方式来做到这一点?

  1. Does Symfony 2 have support for PATCH requests, or any type of partial form content submissions? If so, is there a "correct" (or, more accurately "preferred") way to do this?

除了PRE_BIND(请参见下文)事件方法之外,还有其他其他模式或方法可以解决此问题吗?如果还有其他方法可以解决此问题,那么其中任何一种方法都被认为比其他方法更好或更坏吗?

Aside from the PRE_BIND (see below) event approach, are there any other patterns or ways to solve this problem? If there are other ways of solving this problem, are any of them considered better, or worse, than others?

到目前为止我发现/完成的操作:根据对Github的请求(#5576 ),已经完成了在Symfony 2中支持部分形式绑定的工作(据我了解,以Symfony 2.2为目标).但是,除了该pull请求之外,我找不到任何文档或示例来指示如何使用部分表单绑定.

What I've found/done so far: According to a pull request on Github (#5576), work was done to support partial form bindings in Symfony 2 (from my understanding, targeting Symfony 2.2). However, I can't find any documentation or examples outside of that pull request that indicate how to use partial form bindings.

我找到的一种解决方案可能适合我的目的.该方法是将事件订阅者附加到表单类型的PRE_BIND事件,对stackoverflow的粗略搜索产生以下答案,该答案与我当前正在使用的答案类似:

One solution that I've found may suit my purposes. The approach is to attach an event subscriber to the PRE_BIND event for a form type, a cursory search of stackoverflow yielded the following answer which is similar to one I'm currently employing: https://stackoverflow.com/a/11687863/657674.

推荐答案

对于PATCH请求,Symfony 2.3(也许更早吗?)本机支持部分模型更新.请参阅下面的说明.

For PATCH requests, Symfony 2.3 (maybe earlier?) natively supports partial model updates. See explanation below.

对于非PATCH请求(例如,PUTPOST),您仍然可以通过创建和注册事件订阅者以将未提交的数据处理为原始值来执行部分数据绑定,或者可以写一个自定义请求处理程序,以始终在$clearMissing设置为false的情况下调用$form->submit()方法.

For non-PATCH requests (e.g. PUT and POST), you can still perform partial data binding by either creating and registering an event subscriber to manipulate the unsubmitted data to their original values, or you can write a custom request handler to always call the $form->submit() method with $clearMissing set to false.

在深入研究Symfony的内部,并更好地了解事件订阅者和表单扩展之后,我偶然发现了HttpFoundationRequestHandler类.基本上,从Symfony 2.3开始,绑定表单提交的数据时,开发人员应调用$form->handleRequest($request);而不是在绑定表单时调用$form->submit($request).这将调用附加的请求处理程序(默认为HttpFoundationRequestHandler).请求处理程序可以做一些事情,但最重要的是它如何调用$form->submit().如果请求方法是PATCH告诉表单 not 将未提供的表单数据绑定为null值,则它将false的值传递到表单的submit方法中.

After digging into the internals of Symfony a little bit more, and coming to a better understanding of event subscribers and form extensions, I stumbled upon the HttpFoundationRequestHandler class. Basically, as of Symfony 2.3, instead of calling $form->submit($request) when binding a form's submitted data developers should be calling $form->handleRequest($request); this invokes the attached request handler (by default HttpFoundationRequestHandler). The request handler does a few things, but most importantly is how it calls $form->submit(). It passes a value of false into the form's submit method if the request method was PATCH telling the form not to bind unsupplied form data as null values.

在Symfony 2.3中使用PATCH方法存在一些警告,Symfony的文档可以对此进行进一步解释:

There are some caveats around using the PATCH method in Symfony 2.3 that can be further explained by Symfony's documentation:

如何在路由中GET和POST之外使用HTTP方法:

How to use HTTP Methods beyond GET and POST in Routes:

"不幸的是,生活并非如此简单,因为大多数浏览器不支持发送PUT和DELETE请求.幸运的是,Symfony2为您提供了一种解决此限制的简单方法.查询字符串或HTTP请求的参数,Symfony2将在匹配路由时将其用作方法."

上面来自Symfony文档的报价解释说,大多数浏览器不支持发送PUTPATCHDELETE请求.这是一个问题,因为要使我们利用Symfony的本机支持来进行部分表单更新,我们需要使用PATCH请求.但是,Symfony提供了答案.该文档告诉我们,我们可以使用_method参数或表单值来欺骗我们想要的实际请求,Symfony会确切地知道我们的意思.为了使_method易于理解,您可能必须启用http_method_override配置选项,例如:

The above quote from Symfony's documentation explains that most browsers do not support sending PUT, PATCH, or DELETE requests. Which is a problem because for us to leverage Symfony's native support for partial form updates, we need to use a PATCH request. However, Symfony provides an answer to this. The documentation tells us that we can use the _method parameter or form value to spoof the actual request we want and Symfony will know exactly what we mean. For _method to be understood though, you may have to enable the http_method_override configuration option, e.g.:

# config.yml
framework:
    http_method_override: true
    ...

还有其他方法可以告诉Symfony表单应该使用哪种方法,这些方法可以在以下位置找到:

There are also other ways of telling Symfony what method the form should use, and those can be found here: Changing the Action and Method of a Form.

这篇关于Symfony 2是否支持部分形式绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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