如何在laravel 5.2中添加自定义验证规则?

时间:2016-03-09 15:40:39

标签: php laravel-5 laravel-5.2

我尝试为google recaptcha添加新的验证规则(Laravel 5.2),此规则无效。你能解释一下为什么吗?以及如何解决它?

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('recaptcha', function($attribute, $value, $parameters, $validator) {
            return false;
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

-

class AuthController extends Controller
{
    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
            'g-recaptcha-response' => 'recaptcha|required',
        ]);
    }

要求规则正常工作,不要重新计算。 我尝试做dump-autoload - 没有结果。

非常感谢:)

1 个答案:

答案 0 :(得分:1)

recpatcha|required更改为captcha|required

假设您正在使用https://github.com/anhskohbo/no-captcha