scottie320
scottie320

Reputation: 173

Argument 1 passed to Symfony\Component\Validator\Mapping\GenericMetadata::addConstraint() must be an instance, string given

I set up automatic wiring in the Symfony 3.4 app.

When I tried to change the status of the article during the operation check the error below occurred.

Is it possible that the automatic wiring settings cause this?

Error message:

Type error: Argument 1 passed to 
Symfony\Component\Validator\Mapping\GenericMetadata::addConstraint()
must be an instance of 
Symfony\Component\Validator\Constraint, string given, called in
/home/vagrant/Symfony2/vendor/symfony/symfony/src/Symfony/Component/Validator/Mapping/GenericMetadata.php on line 159

  at Symfony\Component\Validator\Validator\TraceableValidator->validate(object(Coordinate), array('Default', 'Strict'))
     (src/Ahi/Sp/AdminBundle/Model/Service/ArticleService.php:173)
  at App\Ahi\Sp\AdminBundle\Model\Service\ArticleService->App\Ahi\Sp\AdminBundle\Model\Service\{closure}(object(EntityManager))
  at call_user_func(object(Closure), object(EntityManager))
     (vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:240)
  at Doctrine\ORM\EntityManager->transactional(object(Closure))
     (var/cache/dev/jms_diextra/doctrine/EntityManager_602cd1ba7a6bd.php:21)
  at EntityManager602cd1ba7a6bd_546a8d27f194334ee012bfe64f629947b07e4919\__CG__\Doctrine\ORM\EntityManager->transactional(object(Closure))
     (src/Ahi/Sp/AdminBundle/Model/Service/ArticleService.php:183)
  at App\Ahi\Sp\AdminBundle\Model\Service\ArticleService->updateArticleStatus(array('3990'), 'pending', object(Shop))
     (src/Ahi/Sp/AdminBundle/Controller/BaseArticleController.php:434)
  at App\Ahi\Sp\AdminBundle\Controller\BaseArticleController->updateArticleStatusAction(object(Request), object(ArticleService), array('3990'))
     (src/Ahi/Sp/AdminBundle/Controller/Shop/ArticleController.php:137)
  at App\Ahi\Sp\AdminBundle\Controller\Shop\ArticleController->updateArticleStatusAction(object(Request), object(ArticleService), '3990')
 

ArticleService.php

   /**
     * Article status change
     * @param array|integer|string $id ID of the article to be deleted
     * @param $articleStatus
     * @param Shop $shop shop
     * @return integer Number of articles whose status has changed
     */
    public function updateArticleStatus($id, $articleStatus, $shop = null)
    {
        // Article ID for status change
        $ids = is_array($id) ? $id : array($id);

        $ret = $this->entityManager->transactional(function () use ($ids, $articleStatus, $shop) {
            // Obtain validators and validation groups
            $validationGroups = $this->getValidationGroups($articleStatus);

            $count = 0;
            foreach ($ids as $id) {
                $article = $this->getArticle($id, $shop);
                if ($article && $article->getArticleStatus() != $articleStatus) {
                    // Change status
                    $article->setArticleStatus($articleStatus);

                    // Validation
                    //line 173
                    $errors = $this->validator->validate($article, $validationGroups);
                    if (count($errors) > 0) {
                        throw new ArticleValidationException($article, (array)$errors);
                    }

                    // Count articles whose status has changed
                    $count++;
                }
            }
            return $count;
        });

Upvotes: 2

Views: 2297

Answers (2)

Czar Pino
Czar Pino

Reputation: 6314

In my case, it turned out to be an incorrect assumption (on my end) of the arguments of the validate method. The interface of validate method from ValidatorInterface is,

public function validate($value, $constraints = null, $groups = null);

And so,

$error = $validator->validate($entity, $groups);

Should be

$error = $validator->validate($entity, null, $groups);

Upvotes: 3

scottie320
scottie320

Reputation: 173

As shown below, I solved it by changing the validator so that it works only when the parameter is acquired by isset.

                if ($article && $article->getArticleStatus() != $articleStatus) {
                    $article->setArticleStatus($articleStatus);
                   
                    if (isset($article->getPublishDateTime)){
                       $errors = $validator->validate($article, $validationGroups);
                           
                      if (count($errors) > 0) {
                        throw new ArticleValidationException($article, $errors);
                      }
                    }

Upvotes: 0

Related Questions