如何使Symfony回调验证器学到原理? - php

如标题中所述,我实际上需要使用Symfony创建一个验证过程。
我正在使用YAML文件,一切正常。
但是在某些情况下,我需要先检查数据库,然后再说数据已验证。
我在Callback方法中搜索,但实际上只允许我基本上检查这些值。我进行了搜索以进行依赖项注入,甚至将定义的服务作为回调传递,但这样做也无济于事。

简而言之,问题是:能否实现?以何种方式?

参考方案

用@dragoste在评论中说的话,我搜索了如何在自己的约束下实现。
解决方案是使用Custom Constraint。知道要制作什么文件以及要做什么,这有点混乱,所以这就是我所做的。
为了向您解释我的文件,我们的目的是验证租金,而不是根据租金的方式确定,而只是检查同时没有租金。这就是为什么我必须在其中使用带有约束的约束的原因。
在包根目录中创建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类以在其构造函数中接受这些服务即可。
现在,您要做的就是在验证中使用它。
在这里,我们在Resource / config / validation中编辑Rent.yml以仅添加以下行:

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

我们完了!将对象传递给验证程序服务时,验证将起作用。
您可能会注意到这是使用YAML进行的,我个人更喜欢这种处理方式,因为它将每个部分(实体定义,数据库架构,验证文件等)分开,但是您可以使用批注,XML甚至是纯文本来完成。 PHP。这取决于您,因此,如果您想查看更多语法,您仍然可以继续使用Symfony文档链接以了解如何执行此操作。

原则实体管理器和多线程更新数据库 - php

我目前有一个XHR请求,可以从客户端启动N次。该请求由服务器处理,每个请求通常在数据库中创建一个新行(所有原则/ xml)。在我对对象进行persist()之前,请确保是否具有唯一的文件名(我正在上传资产),并且通过重写persist(),调用我的getUniqueFilename()然后调用parent :: persist来做到这一点。当我执行多个带有相…

PHP getallheaders替代 - php

我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…

PHP:将字符串拆分为字母和数字部分的最佳方法 - php

我有几个格式的字符串AA11 AAAAAA1111111 AA1111111 分离字符串的字母和数字部分的最佳方法(最有效)? 参考方案 如果它们都是一系列字母,然后是一系列数字,并且没有非字母数字字符,那么sscanf()可能比regexp更有效$example = 'AAA11111'; list($alpha,$numeric) =…

Symfony 2:如何通过路由名称获取默认路由? - php

是否可以通过名称检索有关某条路线的信息,或获取所有路线的列表?我需要能够获取任意路径的_controller中的defaults值,而不仅仅是当前路径。这有可能吗?P.S .:我发现我可以找到使用YAML的路由,但是重新解析似乎是不必要且繁重的。 参考方案 我真的很擅长回答自己的问题。要获取路由,请在路由器上(控制器内的getRouteCollection(…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…