简体   繁体   English

在Symfony 2中验证没有表单的实体

[英]Validating entities without form in Symfony 2

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. 我正在为Symfony 2创建一个REST API控制器。我开始使用SensioGeneratorBundle创建一个CRUD并修改控制器以充当REST控制器。 However, i don't have forms so i'm thinking about removing this part. 但是,我没有表格所以我正在考虑删除这部分。

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解释为标题和内容。

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. 表单组件负责映射从客户端接收的数据,无论是GET还是POST数据。 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. 得到验证的是实体,而不是形式。

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 . 文档反映了这一点,因为表单验证是symfony 书的不同部分。

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. 您可以将constaints定义为注释或外部文件(yml,php或xml),并使用验证器组件验证实体。 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!');
    }
}

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. 此外,Symfony2的验证组件是完全独立的。 This means you can use it without the whole framework. 这意味着您可以在没有整个框架的情况下使用它。 That being said, when used standalone, you loose nice integration with other stuff. 话虽这么说,当单独使用时,你松散与其他东西的良好集成。

This way, your REST service receives parameters, create entities from it and use the validator service to validate their integrity. 这样,您的REST服务接收参数,从中创建实体并使用验证器服务来验证其完整性。 Using the form is not mandatory to validate entities. 使用表单不是验证实体所必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM