0
votes

I just want to validate API requests using laravel custom Request Handler. For this I have created a RegisterRequest, with the following content.

<?php

namespace App\Http\Requests\Api\Auth;

use App\Http\Requests\Request;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email' => 'required'
        ];
    }
}

And, I want it to use in AuthController as:

use App\Http\Requests\Api\Auth\RegisterRequest;

    public function postRegister(RegisterRequest $request)
    {
        dd($request->all());
    }

But, neither the validation nor the request parameters are shown in the respose. All the routing are working fine, but the query paremeters are empty. Any help will be appriciated.

Version: Laravel: 5.1

I want to let laravel handle all request parameters and say API whether the request is made successful or with errors.

1
You request does not even reach the controller method, so you can't dd anything ;D - naneri

1 Answers

0
votes

Your authorize method should return true. If it is false , RegisterRequest will return forbidden because you are not authorize to use this RegisterRequest class . So, change your authorize method from RegisterRequest to this :

public function authorize()
{
    return true;
}

Edit : If you want to reply the error message in your way you can override the response method in RegisterRequest method like this :

public function response(array $errors)
{
    return \Response::json($errors);
}