類似于Request::only()的東西,如果請求中存在任何額外的/意外的欄位,就會拋出一個錯誤?
例如:
$request-> strictly(['username', 'password'])。
如果POST請求中包含'username'、'password'和'foo'欄位,就會出現錯誤?
如果沒有,那么手動執行這種驗證的最佳方式是什么?
uj5u.com熱心網友回復:
你可以在請求類中使用Macro創建你自己的strictly方法。
AppServiceProvider.php并在boot方法中添加此方法
use IlluminateHttpRequest;
使用 IlluminateValidationValidationException。
public function boot()
{
Request::macro('strict', function(array) {
$array = array_diff(request()->keys(), $paramaters)。)
if (count($array) > 0) {
throw ValidationException::withMessages(
collect($array)->map(function ($row) {
return [$row => 'not needed'] 。
})->collapse()->all()
);
}
});
}
現在你可以在控制器或請求類中使用這個方法
request()->嚴格(['username', 'password'])。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/307821.html
標籤:
