申请表重定向到禁止页面

问题描述:

I followed laravel documentation about Form Request Validation. and im not sure if get it correctly. i created a make:request EmployeeRequest to validate an employee form. but for some reasons it only redirects me to a blank page with "forbidden" as message

// this is my request code
public function rules()
{
    return [
        'email' => 'bail|required|email|unique:employees|max:255',
        'first_name' => 'bail|required|max:50|min:2',
        'last_name' => 'bail|required|max:50|min:2',
        'date_of_birth' => 'bail|required',
        'contact_number' => 'bail|required|max:11',
        'image' => 'bail|required|image',
    ];
}

//then i called it in my controller like this
public function store(EmployeeRequest $request)
{
      //my code if everything is valid
}

(when entering invalid data) it only redirects me to a blank page with an echo of forbidden. im not sure if i understand it correctly. but im expecting that it will automatically redirects to the previous page with errors

Maybe you have to authorize the user, or change the authorize() function to return true . From the docs:

If the authorize method returns false, a HTTP response with a 403 status code will automatically be returned and your controller method will not execute.

If you plan to have authorization logic in another part of your application, return true from the authorize method:

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