php 在 Symfony 2 中验证没有形式的实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10324782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:54:57  来源:igfitidea点击:

Validating entities without form in Symfony 2

phpsymfony

提问by vinnylinux

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.

我正在为 Symfony 2 创建一个 REST API 控制器。我开始使用 SensioGeneratorBundle 创建一个 CRUD 并修改控制器以充当 REST 控制器。但是,我没有表格,所以我正在考虑删除这部分。

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.

如何在没有表单的情况下验证我的字段?一切都与表单相关联,我想要一些自由,包括自定义字段名称。例如,POST x 和 y 字段被 Symfony 解释为标题和内容。

回答by Matt

To be true, form is not directly related to validation. Let me explain this.

诚然,表单与验证没有直接关系。让我解释一下。

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).

表单组件负责映射从客户端接收到的数据,无论是 GET 还是 POST 数据。因此,它会将字符串映射到代码的对象(如果未绑定到实体,则可以是数组)。

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.

在数据映射到实体后,表单使用验证器组件来验证实体。这意味着实体的验证与表单组件完全分离。因此,当表单被验证时,它实际上意味着表单组件验证您的实体而不是表单数据。得到验证的是实体,而不是表单。

The form is use solely to take a string representation and map it to the entity hierarchy. The documentation reflects this as the Formand the Validationare distinct sections of the symfony book.

该表单仅用于获取字符串表示并将其映射到实体层次结构。文档反映了这一点,因为FormValidation是 symfony book 的不同部分。

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 Validationsection of the book:

话虽如此,这也意味着实体的验证可以在表单组件之外轻松完成。您将约束定义为注释或在外部文件(yml、php 或 xml)中,并使用验证器组件来验证您的实体。这里的代码示例取自本书的验证部分:

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!');
    }
}

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.

如您所见,这里没有涉及任何表单,只有一个对象和验证器服务。此外,Symfony2 的验证组件是完全独立的。这意味着您可以在没有整个框架的情况下使用它。话虽如此,当独立使用时,您会失去与其他东西的良好集成。

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.

这样,您的 REST 服务接收参数,从中创建实体并使用验证器服务来验证它们的完整性。使用表单不是验证实体的强制性要求。