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

时间:2015-04-10 07:40:01

标签: php forms validation laravel laravel-5

我在Laravel 5中有一个表单请求,其中包含一个非常简单的自定义验证规则。这很简单,所有它需要做的就是失败。

这是我的表单请求文件,完整:

<?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 ...]

到底是怎么回事?我试图实现此自定义验证的几种不同方法(包括this onethis one),但它们都会因同样的错误而失败。

1 个答案:

答案 0 :(得分:2)

响应方法是可选的,因为你实际上并没有使用它然后摆脱它,这就是造成错误的原因。