首页 > 解决方案 > 未使用自定义表单请求方法调用控制器方法

问题描述

对于表单验证,我做了一个Request classvia php artisan make:request UpdatePlanRequest

但是,在使用UpdatePlanRequeststore 中的类之后,不再调用该方法。

UpdatePlanRequest: _

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {   //TODO: CHECK IF THE PROTOTYPE IDS ARE OWNED BY THE USER (https://stackoverflow.com/questions/42662579/validate-an-array-of-integers/42693970)
        return [
            'start_date' => 'required|date',
            'end_date' => 'required|date|after:start_date',
            'name' => 'required|string'
        ];
    }
}

控制器方法:

use App\Http\Requests\UpdatePlanRequest;

public function store(UpdatePlanRequest $request)
    {
        //
        dd('hello');
    }

如果显示了函数标头store(Request $request) hello,则在该示例中未显示。

根据文档$request->validated();,为了验证目的,需要稍后调用自定义请求类。

标签: laravellaravel-5laravel-5.8

解决方案


你有你的 Request 类是抽象的原因吗?运行时创建的默认类php artisan make:request <name>没有将类定义为抽象类。这似乎对我有用,但在将其声明为抽象时无效。


推荐阅读