0%

Laravel Validate 筆記

Validate 筆記

這樣的寫法

1
2
3
4
5
6
7
8
9
10
11
12
13
$v = \Validator::make($request->all(), [
'input_image' => 'max:1024|mimes:jpeg',
],
[
'max' => '大小請小於1MB(1024KB)',
'mimes' => '請使用JPG檔'
]);
if ($v->fails()) {
return redirect()->back()->withInput($request->input())->withErrors($v->errors());
}
ps.放$v->errors()跟$v都可以,withInput()裡面不一定要放東西

等同於

1
2
3
4
5
6
7
$this->validate($request, [
'input_image' => 'max:1024|mimes:jpeg',
],
[
'max' => '大小請小於1MB(1024KB)',
'mimes' => '請使用JPG檔'
]);

因為在app/Http/Controllers/Controller.php
可以看到use ........ , ValidatesRequests;
原始碼在vendor/laravel/framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php