在 Symfony 2 中验证没有形式的实体 [英] Validating entities without form in Symfony 2

查看:16
本文介绍了在 Symfony 2 中验证没有形式的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 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.

这篇关于在 Symfony 2 中验证没有形式的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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