【发布时间】:2019-08-27 08:46:54
【问题描述】:
对于表单验证,我通过php artisan make:request UpdatePlanRequest 做了一个Request class。
但是,在 store 中使用 UpdatePlanRequest 类后,不再调用该方法。
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,在该示例中不是。
根据docs,为了验证目的,稍后需要自定义请求类调用$request->validated();。
【问题讨论】:
-
您可以编辑您的问题并添加您的路线以及创建表单吗?
-
向我们展示表单本身。
-
这是命名的问题。我做了一些错误的重构,同一件事有不同的术语。
标签: laravel laravel-5 laravel-5.8