Mike
Mike

Reputation: 8877

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

I have a Form Request in Laravel 5 that contains a very simple custom validation rule. It's so simple that all it needs to do is fail.

Here is my Form Request file, in full:

<?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;
        });
    }

}

I would expect this to fail, given that validateCoupon simply returns false. Instead I am getting the following error:

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

What on earth is going on? I have tried to implement several different methods of this custom validation (including this one and this one), but they all fail with this same error.

Upvotes: 3

Views: 2298

Answers (1)

tuscan88
tuscan88

Reputation: 5829

The response method is optional and since you're not actually using it then get rid of it, this is what's causing the error.

Upvotes: 2

Related Questions