我的表单请求有什么问题,让一个简单的验证规则不起作用


What is wrong with my Form Request for a simple validation rule to not work?

我在Laravel 5中有一个Form Request,它包含一个非常简单的自定义验证规则。它很简单,只需要失败。

这是我的表格申请文件,全文:

<?php namespace App'Http'Requests;
use Illuminate'Foundation'Http'FormRequest;
use Response;
use Validator;
class StripeProcessFormRequest extends FormRequest
{
    public function __construct() {
        $this->validator = app('validator');
        $this->validateCoupon($this->validator);
    }
    public function rules()
    {
        return [
            'stripeToken' => 'required',
            'coupon' => 'foobar',
        ];
    }
    public function authorize()
    {
        return 'Auth::check();
    }
    // OPTIONAL OVERRIDE
    public function forbiddenResponse()
    {
        return Response::make('Permission denied!', 403);
    }
    // OPTIONAL OVERRIDE
    public function response( array $errors )
    {
    }
    public function validateCoupon($validator) {
        $validator->extend('foobar', function($attribute, $value, $parameters) {
            //return ! MyModel::where('foobar', $value)->exists();
            return false;
        });
    }
}

我预计这会失败,因为validateCoupon只是返回false。相反,我得到了以下错误:

Whoops, looks like something went wrong.
1/1
ErrorException in HttpResponseException.php line 21:
Argument 1 passed to Illuminate'Http'Exception'HttpResponseException::__construct() must be an instance of Symfony'Component'HttpFoundation'Response, null given, called in /home/ubuntu/workspace/wpcertification/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php on line 95 and defined
in HttpResponseException.php line 21
at HandleExceptions->handleError('4096', 'Argument 1 passed to Illuminate'Http'Exception'HttpResponseException::__construct() must be an instance of Symfony'Component'HttpFoundation'Response, null given, called in /home/ubuntu/workspace/wpcertification/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php on line 95 and defined', '/home/ubuntu/workspace/wpcertification/vendor/laravel/framework/src/Illuminate/Http/Exception/HttpResponseException.php', '21', array()) in HttpResponseException.php line 21
at HttpResponseException->__construct(null) in FormRequest.php line 95
at FormRequest->failedValidation(object(Validator)) in ValidatesWhenResolvedTrait.php line 26
at FormRequest->validate() in ValidationServiceProvider.php line 31
at ValidationServiceProvider->Illuminate'Validation'{closure}(object(StripeProcessFormRequest), object(Application)) in Container.php line 1089
at Container->fireCallbackArray(object(StripeProcessFormRequest), array(object(Closure))) in Container.php line 1052
at Container->fireResolvingCallbacks('App'Http'Requests'StripeProcessFormRequest', object(StripeProcessFormRequest)) in Container.php line 679
at Container->make('App'Http'Requests'StripeProcessFormRequest', array()) in Application.php line 572
at Application->make('App'Http'Requests'StripeProcessFormRequest') in RouteDependencyResolverTrait.php line 58
[... Truncated ...]

到底发生了什么事?我尝试过实现这种自定义验证的几种不同方法(包括这一种和这一种),但它们都因相同的错误而失败。

响应方法是可选的,因为您实际上并没有使用它,所以请删除它,这就是导致错误的原因。