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

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

问题描述

我有一个实体 ArticlePattern ,它具有一个属性 pattern (string)。我需要访问数据库来检查模式是否正确。所以我想定义一个方法 ArticlePattern :: isPatternValid()并添加一个约束(使用Doctrine的注释),它将通过Validator对象的验证来检查isPatternValid是否为true。 / p>

从我在这里读到的内容不是一个好主意,要使一个实体依赖于服务容器,这意味着我无法从ArticlePattern内部访问doctrine服务: :isPatternValid()。



那么如何做一个需要访问数据库的自定义验证约束呢?如何处理这样的情况,我认为很常见的是从实体类访问服务容器的许多问题。



编辑:



好的,谢谢你们,所以答案是一个自定义验证约束

解决方案

验证器对象可以是:




  • 一个简单的对象,根本没有与框架环境的连接。

  • 一个服务(在依赖关系注入容器),只要它有弹性,它可以做绝对的任何东西 Symfony\Component\Validator\ConstraintValidatorInterface



那么你要做什么? / p>


  1. 定义简单的缺点traint

  2. 覆盖 validatedBy()方法返回验证器name( return'my_validator' code>)

  3. 在DIC中定义一个简单的服务:

     < service id =project.validator.myclass =Project\Constraints\MyValidator> 
    <! - 这里的服务定义 - >

    <! - 服务必须被标记 - >
    < tag name =validator.constraint_validatoralias =my_validator/>
    < / service>







< h3> EDIT

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


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

      class MyConstraint ... {
    .. 。

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


  2. 注释验证的类而不是属性: / p>

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

    @ Assert / Email
    private $ email;

    ...
    }


  3. 验证器本身看起来与验证某个属性的情况相同:

      class MyValidator extends ConstraintValidator {
    public function isValid ($ value,Constraint $ constraint){
    // $ value是一个对象,而不是一个属性
    }
    }



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.

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.

EDIT:

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

解决方案

A validator object can be:

  • 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 Symfony\Component\Validator\ConstraintValidatorInterface

So what do you have to do?

  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="Project\Constraints\MyValidator">
        <!-- 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. In your constraint class define the target of that constraint (property / class):

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

  2. Annotate validated class instead of property:

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

  3. 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天全站免登陆