Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When I upgraded the system running Symfony3.0 to version 3.4, the following error occured.
It is a function to change the status in the article to the status selected by pulling down. What else should I consider?

Error Code

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

Code
ArticleValidator.php

 <?php
namespace AhiSpCommonBundleValidatorConstraints;

use SymfonyComponentValidatorConstraint;
use SymfonyComponentValidatorConstraintValidator;

/**
 * Article validator
 */
class ArticleValidator extends ConstraintValidator
{
    /**
     * {@inheritdoc}
     */
    public function validate($article, Constraint $constraint)
    {
        // Whether the expiration date is after the posting date and time
        if ($article->getExpireDateTime() && $article->getExpireDateTime() <= $article->getPublishDateTime()) {
            $this->context->buildViolation("Please enter a date and time after the posting date and time.")
                          ->atPath("expireDateTime");
        }
    }
}

ArticleService.php

/ **
     * Article status change
     *
     * @param array | integer | string $id id of the article to be deleted
     * @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($em) use($ids, $articleStatus, $shop){
            // Get validators and validation groups
            $validator = $this->container->get('validator');
            $validationGroups = $this->getValidationGroups ($articleStatus);

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

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

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

        return gettype($ret) == "integer"? $ret:0;
    }

BaseArticleController.php

/ **
     * Article status change
     * /
    protected function updateArticleStatusAction(Request $request, $ids)
    {
        // Check CSRF token
        $csrf_token = newCsrfToken('authenticate', $token);

        if(! $This->get('security.csrf.token_manager')->isTokenValid($csrf_token)){
            throw new HttpException("400", "The CSRF token is invalid. Please try to resubmit the form.");
        }

        // Check status
        $articleStatus = $request->request->get("articleStatus");
        if(! in_array($articleStatus, Parameters::getArticleStatusKeys())){
            throw new HttpException("400", "articleStatus is invalid.");
        }

        // Change status
        try {
            $ids = explode(',', $ids);
            $count = $this->getArticleService()->updateArticleStatus($ids, $articleStatus, $this->getShop());
            if($count){
                $this->get('session')->getFlashBag()->add('success', "{$count} article stator changed.");
            }
        } catch (ArticleValidationException $e){
            $ article = $e->getArticle();
            $ statusArray = Parameters::getArticleStatus();
            $ this->get('session')->getFlashBag()->add(
                'error',
                sprintf(
                    "Article ID:% d could not be"% s ". Please check your entries. ",
                    $article->getId(),
                    $statusArray[$article->getArticleStatus()]
                )
            );
        }

        // redirect
        $backurl = $request->query->get("backurl");
        if(! $backurl){
            $backurl = $this->generateUrl($this->indexRoute);
        }
        return $this->redirect($backurl);
    }

Validation.yml

AhiSpCommonBundleModelEntityArticle:
    constraints:
        - AhiSpCommonBundleValidatorConstraintsArticle: ~

Services.yml

    common.validator.articleValidator:
        class: AhiSpCommonBundleValidatorConstraintsArticleValidator
        tags:
            - { name: validator.constraint_validator, alias: article }

Version
Symfony 3.4.47 PHP 7.3


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
264 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...