Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3.4 : RecursiveValidator or TraceableValidator?

Tags:

php

symfony

I have a problem with my service.

 <service id="api.api" class="ApiBundle\Service\ApiService">
        <argument type="service" id="request_stack"/>
        <argument type="service" id="validator"/>
 </service>

The __construct is :

public function __construct(RequestStack $requestStack, RecursiveValidator  $validator)
{
    $this->request = $requestStack->getCurrentRequest();
    $this->validator = $validator;
}

The problem is:

  • ENV_DEV, the validator must be an instance of TraceableValidator
  • ENV_PROD, the validator must be an instance of RecursiveValidator

Do you know why I have this conflict ?

In ENV_DEV with RecursiveValidator, I have this error :

Type error: Argument 2 passed to 
ApiBundle\Service\ApiService::__construct() must be an instance of 
Symfony\Component\Validator\Validator\RecursiveValidator, 
instance of Symfony\Component\Validator\Validator\TraceableValidator 
given, called in 
var/cache/dev/ContainerLqjid6c/getApi_ApiService.php on line 8

cache:clear doesn't solve the problem.

Thank you for your help.

like image 818
Skyd Avatar asked Sep 15 '26 18:09

Skyd


1 Answers

Instead of hinting to an implementation, you should always (if available) hint to an interface. In this case, both RecursiveValidator and TraceableValidator implement the ValidatorInterface.

So your constructor should look like this:

public function __construct(RequestStack $requestStack, ValidatorInterface  $validator)
{
    $this->request = $requestStack->getCurrentRequest();
    $this->validator = $validator;
}
like image 67
lxg Avatar answered Sep 18 '26 08:09

lxg