Skip to content
Advertisement

Symfony access headers in custom validator constraint

I am trying to access the headers of my request in a custom validator constraint like so:

use SymfonyComponentValidatorConstraintsCountValidator as BaseCountValidator;

final class CountValidator extends BaseCountValidator
{
    private Request $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function validate($value, Constraint $constraint)
    {
        parent::validate($value, $constraint);
    }
}

But the headerBag in $this->request is always an empty array unfortunately.

Is there maybe an issue because I extend the symfony count validator?

Advertisement

Answer

You should ingest RequestStack instead of Request

See the documentation

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement