>我已经为控制器生成了新的表单请求,但我不知道如何在验证器中处理之前过滤数据等等。
在这种情况下,Laravel中是否有一些本机解决方案?
class TestRequest 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()
{
return [
"title" => "required|string",
"order" => "required|integer"
];
}
}
class TestController extends Controller
{
public function store(TestRequest $request, $chapterId)
{
// some business logic
}
}
Laravel 5.5 中有一些解决方案,但在此示例中作者使用validate
用于从请求中过滤数据,但我需要在 TestRequest 中使用过滤器
$data = $this->validate(request(), [
//...
]); // I can't use this inside TestRequest
您可以使用我的软件包:https://github.com/mikicaivosevic/laravel-filters
它允许您在验证之前过滤请求值...
<?php
class LoginRequest extends FormRequest {
//Filter
public function filters()
{
return [
'name' => 'lower',
'id' => 'int',
];
}
//...
}
- 将$request>名称值转换为小写。
- 将 $request->id 值转换为整数。