'Invalid service "ReCaptcha\ReCaptcha": method "__construct()"
I'm on Symfony 5.4 and I'm trying to make an old tutorial (Symfony4.4)
I need to make a bundle to use Google's ReCaptcha
when I send my form with the valid captcha, I have an error that I do not understand
Invalid service "ReCaptcha\ReCaptcha": method "__construct()" has no argument type-hinted as "ReCaptcha\RequestMethod". Check your service definition.
Codes:
// services.yaml
services:
recaptcha.type:
class: Grafikart\RecaptchaBundle\Type\RecaptchaSubmitType
tags: [ 'form.type' ]
arguments:
$key: '%recaptcha.key%'
recaptcha.validator:
class: Grafikart\RecaptchaBundle\Constraints\RecaptchaValidator
tags: ['validator.constraint_validator']
autowire: true
ReCaptcha\ReCaptcha:
class: ReCaptcha\ReCaptcha
arguments:
$secret: '%recaptcha.secret%'
$requestMethod: '@ReCaptcha\RequestMethod:'
ReCaptcha\RequestMethod: '@ReCaptcha\RequestMethod\CurlPost'
ReCaptcha\RequestMethod\Curl: ~
// RecaptchaValidator.php
<?php
namespace Grafikart\RecaptchaBundle\Constraints;
use ReCaptcha\ReCaptcha;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class RecaptchaValidator extends ConstraintValidator
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var ReCaptcha
*/
private $reCaptcha;
public function __construct(RequestStack $requestStack, ReCaptcha $reCaptcha)
{
$this->requestStack = $requestStack;
$this->reCaptcha = $reCaptcha;
}
public function validate($value, Constraint $constraint)
{
$request = $this->requestStack->getCurrentRequest();
$recaptchaResponse = $request->request->get('g-recaptcha-response');
if (empty($recaptchaResponse)) {
$this->addViolation($constraint);
}
$response = $this->reCaptcha
->setExpectedHostname($request->getHost())
->verify($recaptchaResponse, $request->getClientIp());
if (!$response->isSuccess()) {
$this->addViolation($constraint);
}
}
private function addViolation (Constraint $constraint)
{
$this->context->buildViolation($constraint->message)->addViolation();
}
}
For info, I get the error when I add the last three lines on services.yaml
$requestMethod: '@ReCaptcha\RequestMethod:'
ReCaptcha\RequestMethod: '@ReCaptcha\RequestMethod\CurlPost'
ReCaptcha\RequestMethod\Curl: ~
Thank you for your help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
