简体   繁体   English

Symfony 3 - 如何将验证器注入服务?

[英]Symfony 3 - how to inject the validator into a service?

I try to inject the validator into my service - but I don't find it: 我尝试将验证器注入我的服务 - 但我找不到它:

mybundle.service.supplier:
     class: AppBundle\Service\SupplierService
     calls:
        - [setValidator, ['@validator']]

the @validator is not the expected RecursiveValidator http://api.symfony.com/3.1/Symfony/Component/Validator/Validator/RecursiveValidator.html - the @validator is an interface. @validator不是预期的RecursiveValidator http://api.symfony.com/3.1/Symfony/Component/Validator/Validator/RecursiveValidator.html - @validator是一个接口。

So how can I inject the validator into my service? 那么如何将验证器注入我的服务?

This is what I want: 这就是我要的:

<?php

namespace AppBundle\Service;


use AppBundle\Entity\Supplier;
use AppBundle\Helper\EntityManagerTrait;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Validator\RecursiveValidator;

/**
 * Class SupplierService
 * @package AppBundle\Service
 */
class SupplierService
{
    use EntityManagerTrait;

   /** @var RecursiveValidator $validator */
    protected $validator;

    /**
     * @return RecursiveValidator
     */
    public function getValidator()
    {
        return $this->validator;
    }

    /**
     * @param RecursiveValidator $validator
     * @return SupplierService
     */
    public function setValidator($validator)
    {
        $this->validator = $validator;

        return $this;
    }


    public function addSupplier($data)
    {
        $supplier = new Supplier();
        $validator = $this->getValidator();
        $errors = $validator->validate($supplier);

    }
}

@validator is an interface. @validator是一个接口。

This does not make sense. 这根本不符合逻辑。 If it would be an interface, there could not be an instacne of Validator service. 如果它是一个接口,那么就不会有Validator服务的instacne。 Yes, it does implement ValidatorInterface , but it is not it. 是的,它确实实现了ValidatorInterface ,但它不是。

On the other hand, I am sure you would get an instance of RecursiveValidator . 另一方面,我相信你会得到一个RecursiveValidator实例。 See my analyses: 看我的分析:

  • Check the validator definition in Symfony's XML: 检查Symfony XML中的validator定义:

     <service id="validator" class="Symfony\\Component\\Validator\\Validator\\ValidatorInterface"> <factory service="validator.builder" method="getValidator" /> </service> 
  • Check the definition of validator.builder factory service: 检查validator.builder工厂服务的定义:

     <service id="validator.builder" class="Symfony\\Component\\Validator\\ValidatorBuilderInterface"> <factory class="Symfony\\Component\\Validator\\Validation" method="createValidatorBuilder" /> <call method="setConstraintValidatorFactory"> <argument type="service" id="validator.validator_factory" /> </call> <call method="setTranslator"> <argument type="service" id="translator" /> </call> <call method="setTranslationDomain"> <argument>%validator.translation_domain%</argument> </call> </service> 
  • Check the factory Symfony\\Component\\Validator\\Validation::createValidatorBuilder . 检查工厂Symfony\\Component\\Validator\\Validation::createValidatorBuilder It returns an instance of ValidatorBuilder 它返回ValidatorBuilder一个实例

  • Finally, check the ValidatorBuilder::getValidator() . 最后,检查ValidatorBuilder::getValidator() It ends with the following: 它以下结尾:

     return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers); 

So, you will get the correct instance ( RecursiveValidator ). 因此,您将获得正确的实例( RecursiveValidator )。

Hope this helps... 希望这可以帮助...

In symfony 3.4 simply like this worked: 在symfony 3.4中,就像这样工作:

Config: 配置:

stl.transaction.import.writer.insurance_company:
        class: STL\TransactionBundle\Import\Writer\InsuranceCompanyWriter
        public: false
        arguments:
            - '@validator'

Your service: 您的服务:

use Symfony\Component\Validator\Validator\ValidatorInterface;
// ...
public function __construct(
        ValidatorInterface $validator,
    ) {
        $this->validator = $validator;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM