symfony2表单如何从角度资源接受json负载 [英] symfony2 form how to accept json payload from angular resource
问题描述
我当前正在尝试将Symfony表单与angularJS
组合在一起...服务将数据发布到表单,该表单应将实体保存到数据库中.不幸的是,ngResource
将数据作为JSON有效负载发送,以使Symfony Forms无法对其进行处理...我在角度方面尝试了很多事情,例如更改标头:
I'm currently trying to combine a Symfony Form with angularJS
... A Service posts data to a form, that should save an entity to the database. Unfortunately, ngResource
sends the data as a JSON payload, so that Symfony Forms can't process it... I tried many things on angular side, like changing the headers:
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
我在棱角侧找不到更多的东西,所以我认为我可以在Symfony侧找到解决方案. 知道我如何使它起作用吗?当然也欢迎使用Angular-Solutions.
I couldn't find much more on angular side, so I thought that I could find a Solution on Symfony Side. Any Idea how I could get this to work? Angular-Solutions are welcome too of course.
推荐答案
在深入阅读文档之后,我终于找到了解决方案. Symfony\Component\Form\Form::bind
不需要请求,它也适用于数组.所以这是我的解决方案(草率的方式,需要对标题进行一些检查,以便用于生产.)
I finally found a solution, after reading deeper in the documentation. Symfony\Component\Form\Form::bind
doesn't require a Request, it works with an array too. so here's my Solution (the sloppy way, would need some checking of the header, etc. for production use..)
public function setFooAction(Request $request){
$form = $this->createForm();//get the form class, etc...
$json_data = json_decode($request->getContent(),true);//get the response data as array
$form->bind($json_data); //Bind data to Form
if ($form->isValid()) {
...
}
}
这篇关于symfony2表单如何从角度资源接受json负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!