【发布时间】:2017-03-01 02:47:45
【问题描述】:
我尝试将验证器注入到我的服务中 - 但我没有找到它:
mybundle.service.supplier:
class: AppBundle\Service\SupplierService
calls:
- [setValidator, ['@validator']]
@validator 不是预期的 RecursiveValidator http://api.symfony.com/3.1/Symfony/Component/Validator/Validator/RecursiveValidator.html - @validator 是一个接口。
那么如何将验证器注入到我的服务中?
这就是我想要的:
<?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);
}
}
【问题讨论】:
标签: php validation symfony