在 Core 之外使用 Symfony2 验证器注释 [英] Use Symfony2 Validator Annotations outside of Core

查看:25
本文介绍了在 Core 之外使用 Symfony2 验证器注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何配置 Symfony2 Validator 以在 Core 之外使用注解?

How do you configure Symfony2 Validator to use annotations outside of Core?

在核心中,您将执行以下操作:

In core you would do the following:

$container->loadFromExtension('framework', array(
  'validation' => array(
    'enable_annotations' => true,
  ),
));

取自:http://symfony.com/doc/2.0/book/validation.html#configuration

现在为了使验证工作,规则是在方法 loadValidatorMetadata(ClassMetadata $metadata) 中设置的,它可以工作,但我更喜欢注释.

For now to make validation work the rules are set within the method loadValidatorMetadata(ClassMetadata $metadata), it works but I prefer annotations.

带有验证注释和用于设置验证规则的替代 php 方法的示例实体:

Example Entity with validation annotations and alternative php method to set validation rules:

<?php

namespace Foo\BarBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity(repositoryClass="Foo\BarBundle\Entity\Repository\FooRepository")
 * @ORM\Table(name="foo")
 */
class Foo {


    /**
     * @ORM\Column(type="integer", name="bar")
     * @Assert\Type(
     *     type="integer",
     *     message="The value {{ value }} is not a valid {{ type }}."
     * )
     */
    protected $bar;


    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('bar', new Assert\Type(array(
            'type'    => 'integer',
            'message' => 'The value {{ value }} is not a valid {{ type }}.',
        )));
    }    
}

更新 1

现在的问题似乎是注释没有正确自动加载.

The issue now seems to be that the annotations are not being autoloaded correctly.

我将注释加载到命名空间中:

I load the annotations in to the namespace with:

\Doctrine\Common\Annotations\AnnotationRegistry
::registerAutoloadNamespace("Symfony\Component\Validator\Constraints\\", __DIR__.'/vendor/symfony/validator');

然后当它尝试自动加载注释时,它会查找不存在的 /vendor/symfony/validator/Symfony/Component/Validator/Constraints/Length.php.该文件实际上位于 /vendor/symfony/validator/Constraints/Length.php

Then when it tries to autoload the annotations it looks for /vendor/symfony/validator/Symfony/Component/Validator/Constraints/Length.php which does not exist. The file is actually located at /vendor/symfony/validator/Constraints/Length.php

我可以创建一个 registerLoader() 但宁愿修复代码.当在 Symfony2 Core 中使用 Validator 时,文件位置是正确的.

I could create a registerLoader() but would rather fix the code. When using Validator within Symfony2 Core that file location would be correct.

如何让它正确自动加载或让 composer 将 Symfony2 组件安装到与核心相同的位置?

How do I make it autoload correctly or get composer to install Symfony2 components to the same location as core?

推荐答案

你需要使用 AnnotationRegistry 注册 Autoloader,所以无论你需要 vendor/autoload,例如 bootstrap.php 添加 registerLoader().

You need to register the Autoloader with AnnotationRegistry, so where ever you require vendor/autoload, for example bootstrap.php add the registerLoader().

//Composer libraries
$loader = require_once 'vendor/autoload.php';

\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader([$loader, 'loadClass']);

事实证明,解决方案非常简单.

Turns out the solution is quite straight forward.

这篇关于在 Core 之外使用 Symfony2 验证器注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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