igoshev / laravel-captcha

Captcha integration for the Laravel
https://laravel.bonecms.tech/laravel-captcha
MIT License
79 stars 35 forks source link

Validation fails when using Validator::passes() or Validator::fails() #36

Closed Thungdemo closed 4 years ago

Thungdemo commented 5 years ago

First of all thank you for this great and easy to use captcha package.

VERSION PHP version 7.1.22 Laravel version 5.4 Package version 2.1.0

ISSUE Captcha validation fails when using $validator->passes() or $validator->fails() inside a FormRequest class. I have a request validation class which always throws the 'Invalid code from the image' error

namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;

class MyRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'captcha' => 'required|captcha',
        ];
    }

    public function withValidator($validator)
    {
        if($validator->passes())
        {
            //do something
        }
    }
}

LIKELY CAUSE Running $validator->passes() or $validator->fails() runs the validation again, but the captcha session is likely cleared in the first validation which causes the validation to fail when run for the second time.