Symfony的版本:2.5自定義的驗證不叫@service_container
錯誤
"Catchable Fatal Error: Argument 1 passed to Intermedius\UserBundle\Validator\Constraints\RegisteredEmailValidator::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in D:\Projektek\pricing_tool\backend\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory.php on line 71 and defined in D:\Projektek\pricing_tool\backend\src\Intermedius\UserBundle\Validator\Constraints\RegisteredEmailValidator.php line 22"
RegisteredEmail.php
class RegisteredEmail extends Constraint
{
public $message = "MSG";
}
RegistereEmailValidator.php
class RegisteredEmailValidator extends ConstraintValidator{
public $containerInterface;
function __construct(ContainerInterface $containerInterface)
{
$this->containerInterface = $containerInterface;
}
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof RegisteredEmail) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\RegisteredEmail');
}
}
public function validateBy()
{
return "registered_email";
}
}
services.yml
services:
intermedius.user.validator.registered_email:
class: Intermedius\UserBundle\Validator\Constraints\RegisteredEmail
arguments: [ @service_container ]
tags:
- { name: validator.constraint_validator, alias: registered_email }
仍然不起作用,相同的錯誤 – Lipi 2014-11-06 15:58:27
RegisteredEmailValidator類的名稱空間是什麼?以及爲什麼你需要嚴格地將驗證器緊緊地放在容器中? – 2014-11-06 16:01:08