Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Validator] Dependency injection not working using #[Attribute]#53698

Unanswered
juanwilde asked this question inQ&A
Discussion options

Symfony version(s) affected

5.4

Description

Inject a dependency in a custom validator results in a dependency injection error.

How to reproduce

  • Create a custom constraint
<?phpdeclare(strict_types=1);namespace Service\Adapter\Framework\Validator\Constraint;use Attribute;use Service\Adapter\Framework\Validator\Validator\GtinCheckDigitConstraintValidator;#[Attribute]class GtinCheckDigitConstraint extends CustomConstraint{    public string $message = 'validation.identity.gtin.digit';    public function validatedBy(): string    {        return GtinCheckDigitConstraintValidator::class;    }}
  • Create a custom validator for it with a Logger in the constructor
<?phpdeclare(strict_types=1);namespace Service\Adapter\Framework\Validator\Validator;use Psr\Log\LoggerInterface;use Real\Validator\Gtin\Factory;use Real\Validator\Gtin\NonNormalizable;use Service\Adapter\Framework\Validator\Constraint\GtinCheckDigitConstraint;use Service\Domain\Mapper\ValidationErrorCodeMapper;use Symfony\Component\Validator\Constraint;use Symfony\Component\Validator\ConstraintValidator;use Symfony\Component\Validator\Exception\UnexpectedTypeException;class GtinCheckDigitConstraintValidator extends ConstraintValidator{    public function __construct(        private readonly LoggerInterface $logger    ) {    }    public function validate($value, Constraint $constraint): void    {        $this->logger->info('Test text...');        if (!$constraint instanceof GtinCheckDigitConstraint) {            throw new UnexpectedTypeException($constraint, GtinCheckDigitConstraint::class);        }        try {            Factory::create($value);        } catch (NonNormalizable) {            $this                ->context                ->buildViolation($constraint->message)                ->setCode(ValidationErrorCodeMapper::CUSTOM_IDENTITY_GTIN_CHECK_DIGIT_ERROR)                ->addViolation();        }    }}
  • Then add the configuration inservices.yaml
services:  _defaults:    autowire: true    autoconfigure: true  Service\Adapter\Framework\Validator\Validator\GtinCheckDigitConstraintValidator:    arguments:      $logger: '@logger'
  • Create a test or a simple controller that use this validator. The error looks like this
ArgumentCountError : Too few arguments to function Service\Adapter\Framework\Validator\Validator\GtinCheckDigitConstraintValidator::__construct(), 0 passed in /var/www/vendor/symfony/validator/ConstraintValidatorFactory.php on line 43 and exactly 1 expected

Possible Solution

No response

Additional Context

No response

You must be logged in to vote

Replies: 1 comment 6 replies

Comment options

xabbuh
Jan 31, 2024
Collaborator

I don't know how you construct theConstraintValidatorFactory in your test, but if your validator class uses dependency injection, you need to bootstrap the factory similarly to how it's done in a Symfony application. The FrameworkBundle for example gives you an instance ofContainerConstraintValidatorFactory.

You must be logged in to vote
6 replies
@xabbuh
Comment options

xabbuhJan 31, 2024
Collaborator

Can you create a small example application that allows to reproduce your issue?

@juanwilde
Comment options

Apparently in a brand new project this doesn't happen so probably something related to the config in my other project. I guess this can be closed :)

@LauLaman
Comment options

Hi@juanwilde Did you manage to get this fixed?

I thought i ran in to the same problem.. In my case it was a Case mismatch between loaded and declared class names.

CustomThing vsCustomthingValidator

Once i resolved that Di started to work.

@juanwilde
Comment options

Hi@LauLaman. The problem in the end was that we were injectingValidatorInterfaceFactory into a service to create a newValidatorInterface implementation into a service and this was failing. Instead, we just created the property classes with PHP attributes with the rules and then injectValidatorInterface in our service. If you want, plase paste your configuration and dependency injection logic and I will try to find if our issue is there :)

@BooleanType
Comment options

I had the same error, but the reason was that I accidentally added the#[Attribute] attribute to the validator class also, not only to the constraint class. Moreover, this error was in the code for quite a long time, but for some reason it showed up only after updating Symfony to version 7.3.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
4 participants
@juanwilde@xabbuh@LauLaman@BooleanType
Converted from issue

This discussion was converted from issue #53697 on January 31, 2024 11:20.


[8]ページ先頭

©2009-2025 Movatter.jp