Validator扩展隐式不在AppServiceProvider中触发

时间:2017-08-15 10:28:19

标签: php laravel validation recaptcha

我试图通过发布到网址提供来验证我的表格上的重新回复回复,但是当我尝试通过AppServiceProvider发送它时,它根本没有点击它并跳过它。我可以看到验证器正在使用FormRequest但由于某种原因它没有触发我的CaptchaValidator上的Check函数

这是我的AppServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \Schema::defaultStringLength(191);

        if ($this->app->environment() == 'production') {
            \URL::forceScheme('https');
        }

        \Validator::extendImplicit('recaptcha', 'App\Validation\CaptchaValidator@Check',
            'You have not passed the recaptcha.');
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        if ($this->app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
    }
}

CaptchaValidator

<?php

namespace App\Validation;

use GuzzleHttp\Client;

class CaptchaValidator
{
    public function Check($attribute, $value, $parameters)
    {
        $parameters = http_build_query([
            'secret' => env('RECAPTCHA_PRIVATE_KEY'),
            'remoteip' => app('request')->getClientIp(),
            'response' => $value,
        ]);

        $url = 'https://www.google.com/recaptcha/api/siteverify?' . $parameters;

        $client = new Client();
        $response = $client->request('post', $url);

        try {
            $results = json_decode($response->getBody()->getContents());

            return $results->success;
        } catch (\Exception $exception) {
        }

        return true;
    }
}

请求包含验证的表单

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RedeemRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'g-recaptcha-response' => 'required|recaptcha',
        ];
    }
}

0 个答案:

没有答案