ARCANEDEV / noCAPTCHA

:passport_control: Helper for Google's new noCAPTCHA (reCAPTCHA v2 & v3)
MIT License
358 stars 56 forks source link

Multiple noCaptcha #27

Closed pns2050 closed 8 years ago

pns2050 commented 8 years ago

Hello,

Firstly thanks for the plugin...

I am trying to add multiple captchas on a single page with laravel 4.2 using:

 public function scriptWithCallback(array $captchas, $callbackName = 'captchaRenderCallback')

It does work but i need to refresh/reload the page for the captchas to render/appear in the page... I tried grepcaptcha.reset(); with no luck...

Any ideas on how to deal with this? Thank you

arcanedev-maroc commented 8 years ago

You're welcome :+1:,

I don't think this issue is related to the package.

Did you followed this example : https://github.com/ARCANEDEV/noCAPTCHA/blob/master/examples/multiple-captchas.php ?

Can you show me your form code ?

pns2050 commented 8 years ago

I followed your installation guide, published configuration for laravel 4.2..

On my blade view:

Form (1)

{{ Form::captcha(['id' => 'captcha1']) }}

Form (2)

{{ Form::captcha(['id' => 'captcha2']) }}

And as my mother told me this one at the bottom of the view ;p

{{ Captcha::scriptWithCallback(['captcha1', 'captcha2']) }}

Now if i navigate to the page, the captchas will not show...(no errors in console). If i reload/refresh the page they do show up , and they work as expected...

Thank you

arcanedev-maroc commented 8 years ago

Ok, i'm going to test this in real application and see what the problem is.

It's probably a weird behavior of the google recaptacha plugin.

You can check this with your source code and see if it solve your issue: http://mycodde.blogspot.com/2014/12/multiple-recaptcha-demo-same-page.html

arcanedev-maroc commented 8 years ago

Hi @pns2050,

I've just checked the no-captcha package with a new fresh laravel 4.2 installation and it works without breaking a sweat.

This is what i've done:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Google noCaptcha</title>
</head>
<body>

    <h1>Form 1</h1>

    {{ Form::open(['method' => 'POST'])}}
        {{ Form::captcha(['id' => 'captcha1']) }}
        {{ Form::submit('Submit form 1')}}
    {{ Form::close() }}

    <h1>Form 2</h1>

    {{ Form::open(['method' => 'POST']) }}
        {{ Form::captcha(['id' => 'captcha2']) }}
        {{ Form::submit('Submit form 2')}}
    {{ Form::close() }}

    {{ Captcha::scriptWithCallback(['captcha1', 'captcha2']) }}
</body>
</html>

Et voila:

captchas

I think you've something that prevent your captchas to be rendered properly (Other javascripts, your browser or something).

pns2050 commented 8 years ago

Alright , thanks for taking the time to check... i ll try to find if there are any conflicts with other javascripts that i'm using...

arcanedev-maroc commented 8 years ago

No problem. Let me know if you fixed your issue (it may helps other artisans).

Good luck :+1:

pns2050 commented 8 years ago

Hello again...

After having a look on google documentation: https://developers.google.com/recaptcha/docs/display#explicit_render

Note : your onload callback function must be defined before the reCAPTCHA API loads. To ensure there are no race conditions: order your scripts with the callback first, and then reCAPTCHA use the async and defer parameters in the script tags

I just made a small change on the script rendering function to re-order the scripts and everything works as expected for me now...

public function scriptWithCallback(array $captchas, $callbackName = 'captchaRenderCallback')
{
    $script = $this->script($callbackName);

    if (empty($script) || empty($captchas)) {
        return $script;
    }

    return implode(PHP_EOL, [implode(PHP_EOL, [
        '<script>',
            "var $callbackName = function() {",
                $this->renderCaptchas($captchas),
            '};',
        '</script>'
    ]), $script]);
}

Would it be possible to have this update on the repo ?

Thanks again!

arcanedev-maroc commented 8 years ago

@pns2050,

OK, i'm going to test this and make sure this change doesn't break the package for the other users.

You're using "arcanedev/no-captcha": "~1.2", right ?

Thank you for your feedbacks

pns2050 commented 8 years ago

Yeh its the ~1.2 for laravel 4.2... But i guess it would be good to have this change for laravel 5 also...:~p

Thank you

arcanedev-maroc commented 8 years ago

Fixed, try to update the package to >= 1.2.7.

I'm going to do the same thing for the other versions (Laravel 5.0 & 5.1).