我已经为控制器生成了新表单Request,但我不知道如何在验证器中处理之前过滤数据等等。
在这种情况下,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中有一些solution但在本例作者中使用
validate
用于过滤来自请求的数据,但我需要在TestRequest中使用过滤器
$data = $this->validate(request(), [
//...
]); // I can't use this inside TestRequest
答案 0 :(得分:0)
您可以使用我的套餐:https://github.com/mikicaivosevic/laravel-filters
它允许您在验证之前过滤请求值...
<?php
class LoginRequest extends FormRequest {
//Filter
public function filters()
{
return [
'name' => 'lower',
'id' => 'int',
];
}
//...
}