我正在尝试在我自己的服务中使用全局验证器服务,如下所示:
# app/config/services.yml
services:
app.handler.user:
class: AppBundle\Handler\UserHandler
arguments: ["@doctrine.orm.entity_manager", "@validator"]
# AppBundle/Handler/UserHandler.php
namespace AppBundle\Handler;
use Doctrine\ORM\EntityManager;
use AppBundle\Entity\User;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class UserHandler
{
public function __construct(EntityManager $entityManager, ValidatorInterface $validator)
{
$this->em = $entityManager;
$this->validator = $validator;
}
public function add(User $user)
{
$errors = $this->validator->validate($user);
return $errors;
}
}
但是当我调用validate方法时,它会抛出错误:
严格的电子邮件验证需要 egulias/email-validator
我使用的 Symfony 版本是 3.1。是否不推荐在 Symfony 3.0+
中使用验证器服务?