如何让 Symfony 表单与 REST API 一起使用? [英] How do I get Symfony forms to work with a REST API?

查看:32
本文介绍了如何让 Symfony 表单与 REST API 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试让 symfony 表单与我的帖子一起使用并放置 rest api 端点.

I'm currently trying to get symfony forms to work with my post and put rest api endpoint.

目前我有:

$request = $this->getRequest();
$cc = new CreditCard();
$form = $this->createForm(new CreditCardType(), $cc);

$form->handleRequest($request);

if ($form->isValid()) {
  //...
}

但是,表单永远不会有效.查看问题,似乎表单的 isSubmitted 字段为 false,因此它从未通过验证.另外,由于这是一个 api 调用,我将 creditcardtype 中的 csrf_protection 设置为 false.

However, the form is never valid. Looking into the problem, it seems that the form's isSubmitted field is false, so it never passes validation. Also, since this is an api call, I have the csrf_protection set to false in the creditcardtype.

以下是我让用户提交的数据示例:

Here is an example of what data I'm having the user submit:

{
  "credit_card": {
     "name" : "Example Name"
     ...
  }
}

我如何让这个 post/put 请求被注册为已提交并让表单验证器通过?谢谢.

How do I get this post/put request to be registered as submitted and have the form validator pass? Thanks.

推荐答案

如果您能提供 CreditCardType 类的完整代码以及完整的 POST 数据和表单中的验证错误,将会很有帮助.

It would be helpful if you could provide the whole code of your CreditCardType class as well as the complete POST data and the validation errors from your form.

无论如何,这是我发现表单提交失败的原因:

Anyway this are the things I found to be the reason for form submit failures:

  • 提交不完整的数据:如果您的 CerditCardType 表单中有一个字段在您的 POST 请求中不存在,请确保其将选项必需"设置为 false.
  • 表单名称:确保所有字段都由以表单名称(您在 CreditCardType::getName() 方法中提供的名称)命名的属性包装.在你的情况下,我假设是credit_card".
  • 仅发布在您的 CreditCardType 类中定义的字段.如果您发布的参数与表单的任何字段都不匹配,您将收到验证错误,例如此表单不应包含任何额外字段"或类似内容.

无论如何,如果您提供更多信息,我很乐意提供更多帮助.

Anyway if you provide more information I'd be glad to help more.

更新:

好的,我认为您的问题是您将数据作为 JSON 字符串发布,而 handleRequest() 方法没有获取任何数据,因为 Symfony 不知道应该从请求的正文(它希望将数据作为 $_GET 或 $_POST 参数发送).所以你必须自己做.

Ok, I think your problem is you are posting your data as a JSON string and the handleRequest() method is not getting any data because Symfony doesn't know it should be decoded from the request's body (it expects the data to be sent either as $_GET or $_POST parameters). So you have to do it on your own.

您必须使用 submit() 方法,而不是使用 handleRequest() 方法,通过解码请求内容获得的数据 ($request->getContent()):

Instead of using the handleRequest() method you have to use the submit() method with the data you obtained by decoding the request content ($request->getContent()):

$request = $this->getRequest();
$cc = new CreditCard();
$form = $this->createForm(new CreditCardType(), $cc);

// Decode de JSON input
$data = json_decode($request->getContent(), true);

// Post the data to the form
$form->submit($data);

if ($form->isValid()) {
  //...
}

希望它现在有帮助!

顺便说一句,如果您正在研究 RESTful 实现,您真的应该考虑使用 FOSRestBundle 捆绑.它将为您处理所有格式转换逻辑以及路由、实体反/序列化等.

BTW if your are working on an RESTful implementation you should really consider using the FOSRestBundle bundle. It would handle for you all the formats conversion logic as well as routing, entities de/serialization, etc.

这篇关于如何让 Symfony 表单与 REST API 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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