Why my symfony captcha always return valid?

I followed this tutorial to add a captcha to my form.

First I install it using

composer require captcha-com/symfony-captcha-bundle:"4.*"

After I Install it, I got an error on a file called captcha.html.twig
Error:

captcha Unexpected "spaceless" tag (expecting closing tag for the
"block" tag defined near line 2).

So I changed {% spaceless %} to {% apply spaceless %}
And the error is gone.

But My captcha bot always returns True (even when I don’t even fill it.
Here is the ->add() function of my form:

->add('captchaCode', CaptchaType::class, [
    'captchaConfig' => 'ExampleCaptchaUserRegistration',
    'constraints' => [
        new ValidCaptcha([
            'message' => 'Invalid captcha, please try again',
        ]),
    ],]);

Code of Routes.yaml :

captcha_routing:
    resource: "@CaptchaBundle/Resources/config/routing.yml"

Code of captcha.php :

<?php 
// app/config/packages/captcha.php
if (!class_exists('CaptchaConfiguration')) { return; }

// BotDetect PHP Captcha configuration options
return [
    // Captcha configuration for example form
    'ExampleCaptchaUserRegistration' => [
        'UserInputID' => 'captchaCode',
        'ImageWidth' => 250,
        'ImageHeight' => 50,
    ],
];

Captcha field on the User.php entity:

protected $captchaCode;

public function getCaptchaCode()
{
  return $this->captchaCode;
}

public function setCaptchaCode($captchaCode)
{
  $this->captchaCode = $captchaCode;
}

Code in the View (twig)

<span class="txt1 p-b-11">
    Are You a Robot?
</span>
<div class="wrap-input100 m-b-36">
    {{ form_widget(form.captchaCode, {'attr': {'class': 'input100'} }) }}       
    <span class="focus-input100"></span>
</div>
<div class="error">{{ form_errors(form.captchaCode) }} </div>

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *