ARCANEDEV / noCAPTCHA

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

reCAPTCHA v2 issue #94

Closed KumaChenGC closed 4 years ago

KumaChenGC commented 4 years ago

Description:

Route:

Route::get('captcha', 'CaptchaController@index');
Route::post('captcha', 'CaptchaController@captcha');

View:

<!DOCTYPE html>
<html lang="jp">
<head>
    <meta charset="UTF-8">
    <title>Captcha</title>
</head>
<body>
    {{ $msg ?? '' }}
    <form action="{{ action('CaptchaController@captcha') }}" method="POST">
        @csrf
        {!! no_captcha()->display() !!}<br />
        @if ($errors->has('g-recaptcha-response'))
            {{ $errors->first('g-recaptcha-response') }}<br />
        @endif
        <button type="submit">Send</button>
    </form>
    {{-- API --}}
    {!! no_captcha()->script()->toHtml() !!}
</body>
</html>

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use View;
use Validator;
use Arcanedev\NoCaptcha\Rules\CaptchaRule;

class CaptchaController extends Controller
{
    public function index(){
        return View::make('captcha');
    }
    public function captcha(Request $request){
        $rules = [
            'g-recaptcha-response' => ['required', new CaptchaRule],
        ];
        $messages = [
            'g-recaptcha-response.required' => 'Not verified yet',
            'g-recaptcha-response.captcha'  => 'verification failed',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if($validator->fails()){
            return View::make('captcha')->withErrors($validator->messages());
        }else{
            return View::make('captcha')->with([
                'msg' => 'Success'
            ]);
        }
    }
}

Config:

// config/no-captcha.php
'version'=>'v2'

Steps To Reproduce:

When i enter http://localhost/public/captcha

alert: Call to undefined function no_captcha() (View: /var/www/html/laravel/resources/views/captcha.blade.php)

arcanedev-maroc commented 4 years ago

I believe that no_captcha() helper doesn't exists in the v7.x

Try to select the correct branch: https://github.com/ARCANEDEV/noCAPTCHA/tree/v7.x

To fix your issue, use the Captcha facade instead of no_captcha() helper.

Example: https://github.com/ARCANEDEV/noCAPTCHA/blob/v7.x/_docs/3-Usage.md#views