0
votes

Symfony version: 2.5

Error

"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 }
2

2 Answers

0
votes

The class of your validator service definition should be set to the validator class, not the constraint.

class: Intermedius\UserBundle\Validator\Constraints\RegisteredEmailValidator
0
votes

You should not inject the service_container in your services. In your example you should rather inject only the services you need instead. See for example the "Avoiding your Code Becoming Dependent on the Container" section of http://symfony.com/doc/current/components/dependency_injection.html.