Symfony2/Doctrine - 需要访问数据库的验证约束 [英] Symfony2/Doctrine - validation constraint which requires access to a database

查看:17
本文介绍了Symfony2/Doctrine - 需要访问数据库的验证约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体ArticlePattern,它有一个属性pattern(字符串).我需要访问数据库以检查模式是否正确.所以我想定义一个方法 ArticlePattern::isPatternValid() 并添加一个约束(使用 Doctrine 的注释),它会在 Validator 对象验证期间检查 isPatternValid 是否为真.

I have an entity ArticlePattern, which has a property pattern (string). I need to access the database to check if pattern is correct. So I would like to define a method ArticlePattern::isPatternValid() and add a constraint (using Doctrine's annotation) which would check if isPatternValid is true during validation by Validator object.

从我在这里和那里阅读的内容来看,使实体依赖于服务容器并不是一个好主意,这意味着我无法从 ArticlePattern::isPatternValid() 内部访问学说服务.

From what I have read here and there it is not a good idea, to make an entity depended on service container, which mean I cannot access the doctrine service from inside ArticlePattern::isPatternValid().

那么我怎样才能创建一个需要访问数据库的自定义验证约束呢?你如何处理我认为很常见的这种情况,看到这么多关于从实体类访问服务容器的问题.

So how can I make a custom validation constraint which need an access to the database? How do you deal with such situations which I think is very common seeing so many questions about accessing a service container from an entity class.

好的,谢谢大家,所以答案是 自定义验证约束

Ok, thanks guys, so the answer is a Custom Validation Constraint

推荐答案

验证器对象可以是:

  • 一个简单的对象,与框架环境完全没有联系.
  • 可以做任何事情的服务(在 依赖注入容器的上下文中)只要它实现 SymfonyComponentValidatorConstraintValidatorInterface
  • A simple object, that has no connection to the framework environment at all.
  • A service (in the context of dependency injection container) which could do absolutley anything as long as it impements SymfonyComponentValidatorConstraintValidatorInterface

那你要做什么?

  1. 定义一个简单的约束
  2. 重写 validatedBy() 方法以返回验证器名称"(return 'my_validator';)
  3. 在 DIC 中定义一个简单的服务:

  1. Define a simple constraint
  2. Override validatedBy() method to return validator "name" (return 'my_validator';)
  3. Define a simple service in DIC:

<service id="project.validator.my" class="ProjectConstraintsMyValidator">
    <!-- service definition here -->

    <!-- the service has to be tagged -->
    <tag name="validator.constraint_validator" alias="my_validator" />
</service>

<小时>

编辑

您询问了多个属性验证.在这种情况下,您可以创建一个与对象相关而不是与对象属性相关的验证器.


EDIT

You've asked about multiple properties validation. In such a case you could create a validator that is related to the object rather to the property of the object.

  1. 在您的约束类中定义该约束的目标(属性/类):

  1. In your constraint class define the target of that constraint (property / class):

class MyConstraint ... {
    ...

    public function targets() {
        return self::CLASS_CONSTRAINT;
    }
}

  • 注释已验证的类而不是属性:

  • Annotate validated class instead of property:

    @Assert/MyConstraint(...)
    class MyClass {
        private $firstName;
        private $lastName;
    
        @Assert/Email
        private $email;
    
        ...
    }
    

  • 验证器本身看起来与验证属性的情况几乎相同:

  • The validator itself looks pretty much the same as in case of validating a property:

    class MyValidator extends ConstraintValidator {
        public function isValid($value, Constraint $constraint) {
            // $value is an object rather a property
        }
    }
    

  • 这篇关于Symfony2/Doctrine - 需要访问数据库的验证约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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