如何使Symfony Callback验证器原理知道?

时间:2016-10-05 06:22:38

标签: php validation doctrine-orm symfony

如标题中所述,我实际上需要使用Symfony创建验证过程。 我使用YAML文件,一切都很好。 但在某些情况下,我需要先检查数据库,然后才能验证数据。 我在Callback方法中搜索,但它实际上只允许我基本检查值。我搜索了依赖注入,甚至将定义的服务作为回调传递,但它也没有帮助。

所以问题,简而言之就是:是否有可能实现它?以哪种方式?

1 个答案:

答案 0 :(得分:5)

@dragoste在评论中说过,我搜索了如何用自己的约束来制作它。

解决方案是使用Custom Constraint。知道要制作什么文件和做什么有点麻烦,所以这就是我所做的。

为了向您解释我的文件是什么,目标是验证租金,而不是如何制作,但只是检查同一时刻没有租金。这就是为什么我必须在其中使用带有Doctrine的约束。

在捆绑包的根目录中创建Validator文件夹。然后,在Validator文件夹中添加Constraints文件夹。

Validaor / Constraints 文件夹中创建文件 RentDatesConstraint.php 。 以下是它的外观:

<?php

namespace ApiBundle\Validator\Constraints;

use ApiBundle\Validator\RentDatesValidator;
use Symfony\Component\Validator\Constraint;

class RentDatesConstraint extends Constraint
{
    public $message = 'The beginning and ending date of the rent are not available for this vehicle.'; // note that you could use parameters inside it, by naming it with % to surround it

    /**
     * @inheritdoc
     */
    public function validatedBy()
    {
        return RentDatesValidator::class; // this is the name of the class that will be triggered when you need to validate this constraint
    }

    /**
     * @inheritdoc
     */
    public function getTargets()
    {
        return self::CLASS_CONSTRAINT; // says that this constraints is a class constraint
    }
}

现在您已经创建了自己的类约束,您必须创建自己的验证器。

Validator 文件夹中创建 RentDatesValidator.php 文件。

<?php

namespace ApiBundle\Validator;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class RentDatesValidator extends ConstraintValidator
{
    /**
     * @var Registry $doctrine
     */
    private $doctrine;

    /**
     * RentDatesValidator constructor.
     * @param Registry $_doctrine
     */
    public function __construct(Registry $_doctrine)
    {
        $this
            ->setDoctrine($_doctrine)
        ;
    }

    /**
     * @param Registry $_doctrine
     * @return $this
     */
    public function setDoctrine(Registry $_doctrine)
    {
        $this->doctrine = $_doctrine;
        return $this;
    }

    /**
     * @inheritdoc
     * @param Rent $_value
     */
    public function validate($_value, Constraint $_constraint)
    {
        //do your stuff here

        if ($testFails) {
            $this
                ->context
                ->buildViolation($_constraint->message) // here you can pass an array to set the parameters of the string, surrounded by %
                ->addViolation()
            ;
        }
    }
}

我们差不多完成了,我们必须将其声明为服务,因此我们在 Resources / config

中编辑 services.yml
services:
    # [...]
    validator.rent_dates:
        class: ApiBundle\Validator\RentDatesValidator
        tags:
            - { name: validator.constraint_validator }
        arguments: [ "@doctrine" ]

你可以在这里注意到我通过了 @doctrine 服务,但只要你定义 RentDatesValidator 类,你就可以实际传递任何你想要的服务,甚至很多正确地在其构造函数中接受这些服务。

现在,您所要做的就是在验证中使用它。 在这里,我们在资源/配置/验证中编辑 Rent.yml 以添加此行:

ApiBundle\Entity\Rent:
    constraints:
        - ApiBundle\Validator\Constraints\RentDatesConstraint: ~

我们完成了!将对象传递给验证器服务时,验证将起作用。

你可以注意到这是用YAML制作的,我个人更喜欢这种做事方式,因为它将每个部分(实体定义,数据库模式,验证文件......)分开,但是你可以用注释来做到这一点,XML甚至纯PHP。这取决于您,所以如果您想要查看更多语法,您仍然可以访问Symfony文档的链接以了解如何执行此操作。