excelwebzone / EWZRecaptchaBundle

This bundle provides easy reCAPTCHA form field for Symfony.
MIT License
396 stars 160 forks source link

constant RECAPTCHA_API_JS_SERVER not found #210

Open dmerchier opened 6 years ago

dmerchier commented 6 years ago

I would like to use ReCaptcha for the login page of my Sf3.4 project. When using FosUserBundle, the login form is not a Symfony FormType, so I tried to use the twig field using Javascript. The problem is this script uses an inexistant constant:

<div id="recaptcha-container"></div>
<script type="text/javascript">
    $(document).ready(function() {
        $.getScript("{{ constant('\\EWZ\\Bundle\\RecaptchaBundle\\Form\\Type\\EWZRecaptchaType::RECAPTCHA_API_JS_SERVER') }}", function() {
            Recaptcha.create("{{ form.recaptcha.get('public_key') }}", "recaptcha-container", {
                theme: "clean"
            });
        });
    });
</script>

What should I do if I want to use ReCaptcha without a FormType ? Thank you for your help.

PS : I'm using excelwebzone/recaptcha-bundle v1.5.11

vellmur commented 6 years ago

Same problem in my project. Looks like given form object = '\EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType' haven't any constants at all. Need a way to get value from protected $recaptchaApiJsServer instead of this const. But how to do it by using twig without form object?

vellmur commented 6 years ago

By history of '\EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType', value of CONST was equal to: const RECAPTCHA_API_JS_SERVER = '//www.google.com/recaptcha/api/js/recaptcha_ajax.js';

So not needed to use it this way, you can just paste this link inside of $.getScript():

$.getScript("http://www.google.com/recaptcha/api/js/recaptcha_ajax.js", function() {
    Recaptcha.create("recaptchaKey", "recaptcha-container",{}); 
});

But i fixed this problem just by using html code without using of $.getScript():

 <script src='https://www.google.com/recaptcha/api.js'></script>
 <div class="g-recaptcha" data-sitekey="{{ recaptchaKey }}"></div>

Looks like recaptcha library automatically append all needed functionality by detecting ".g-recaptcha" class.

Also, you can define recaptchaKey inside global variables of Twig in config.yml:

twig:        
    globals:
        recaptchaKey: 'YourSecretKey'`

And documentation must be updated, because 'EWZRecaptchaType' doesn't have conts variables after 1.5.5 tag.