我创建了一个名为 ClientStoreRequest
的自定义表单请求使用以下代码:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ClientStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
// return $this->user()->can('add-clients');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|unique:clients|max:255',
'website' => 'required|url',
'street' => 'required',
'town' => 'required',
'postcode' => 'required|max:8',
'county' => 'required',
'country' => 'required'
];
}
}
然后我修改了我的
ClientController
的 store
方法看起来像这样: /**
* Store a newly created resource in storage.
*
* @param ClientStoreRequest $request
* @return \Illuminate\Http\Response
*/
public function store(ClientStoreRequest $request)
{
dd(1);
}
所以当表单提交时,它应该杀死页面并打印一个
1
到屏幕。当我使用 ClientStoreRequest $request
,它只是让我回到我提交表单的页面,没有错误,也没有 dd(1)
结果,但是当我使用 Request $request
时它打印 1
到屏幕。我错过了一些非常明显的东西吗?我已经 followed the docs做这个。
编辑:我正在使用资源 Controller ,所以路由是
Route::resource('clients', 'ClientController');
最佳答案
有点尴尬,但这完全归咎于开发人员的错误。自定义表单请求实际上工作正常......只是我的规则引用了我忘记放入表单的一个字段,所以没有在屏幕上显示错误!一旦我回显了通常的 $errors 数组,我就可以看到我的错误 - 我会责怪深夜编码!
关于php - Laravel 5.8 自定义表单请求未按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57022841/