在 Symfony 2 验证没有形式的实体
![用户头像](/static/whole/images/alert/data-load/load-15.gif)
问题说明
我正在为 Symfony 2 创建一个 REST API 控制器.我开始使用 SensioGeneratorBundle 创建一个 CRUD 并修改控制器以充当 REST 控制器.但是,我没有表格,所以我正在考虑删除这部分.
I'm creating a REST API controller for Symfony 2. I started using the SensioGeneratorBundle to create a CRUD and modified the controller to act as a REST controller. However, i don't have forms so i'm thinking about removing this part.
如何在没有表单的情况下验证我的字段?一切都与表单相关联,我想要一些自由,包括自定义字段名称.例如,POST x 和 y 字段被 Symfony 解释为标题和内容.
How can i validate my fields without a form? Everything is connected to the form and i want some freedom, including customizing field names. For example, POST x and y fields are interpreted by Symfony as title and content.
正确答案
确实,表单与验证没有直接关系.让我解释一下.
To be true, form is not directly related to validation. Let me explain this.
表单组件负责映射从客户端接收到的数据,无论是 GET 还是 POST 数据.因此,它会将字符串映射到代码的对象(如果不绑定到实体,则可以是数组).
The form component is responsible of mapping data received from the client, be it GET or POST data. So, it will maps string to object of your code (can be an array if not binding to an entity).
在数据映射到实体后,表单使用验证器组件来验证实体.这意味着实体的验证与表单组件完全分离.因此,当表单被验证时,它实际上意味着表单组件验证您的实体而不是表单数据.得到验证的是实体,而不是表单.
Form use the validator component to validate the entity after data have been mapped to it. This means that validation of the entity is totally decoupled from the form component. So, when the form is validated, it really means that the form component validate your entity and not the form data. What gets validated is the entity, not the form.
表单仅用于获取字符串表示并将其映射到实体层次结构.文档将此反映为 Form 和 验证 是 symfony 书籍.
The form is use solely to take a string representation and map it to the entity hierarchy. The documentation reflects this as the Form and the Validation are distinct sections of the symfony book.
话虽如此,这也意味着实体的验证可以在表单组件之外轻松完成.您将约束定义为注释或在外部文件(yml、php 或 xml)中,并使用验证器组件来验证您的实体.这里的代码示例取自 Validation 部分本书:
That being said, this also means that the validation of entities can be done outside the form component at great ease. You define you constaints as annotations or in an external file (yml, php or xml) and use the validator component to validate you entity. Here a code example taken from the Validation section of the book:
use Symfony\Component\HttpFoundation\Response;
use Acme\BlogBundle\Entity\Author;
// ...
public function indexAction()
{
$author = new Author();
// ... do something to the $author object
$validator = $this->get('validator');
$errors = $validator->validate($author);
if (count($errors) > 0) {
return new Response(print_r($errors, true));
} else {
return new Response('The author is valid! Yes!');
}
}
如您所见,这里没有涉及任何表单,只有一个对象和验证器服务.此外,Symfony2 的验证组件是完全独立的.这意味着您可以在没有整个框架的情况下使用它.话虽如此,当单独使用时,您会失去与其他东西的良好集成.
As you can see, there is no form involved here, only an object and the validator service. Moreover, the validation component of Symfony2 is completely standalone. This means you can use it without the whole framework. That being said, when used standalone, you loose nice integration with other stuff.
通过这种方式,您的 REST 服务接收参数,从中创建实体并使用验证器服务来验证它们的完整性.使用表单不是验证实体的强制性要求.
This way, your REST service receives parameters, create entities from it and use the validator service to validate their integrity. Using the form is not mandatory to validate entities.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhecegjh
-
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
复制文件夹/文件而不修改属性?
it1352 07-15 -
为什么字符'^'被Python Popen忽略 - 在Popen Windows转义'^'字符?
it1352 08-07