讓我驚異的一段代碼:
$this->validate(request(), [
'user.name' => 'sometimes|required|exists:users,name',
]);
上面的代碼表示需要 users 表中的 name 字段存在用戶提交的值,這個 Validation 機制在使用的時候感覺耳目一新,所以這里系統(tǒng)的學習一下 Laravel Validation
文檔地址:
https://laravel.com/docs/5.5/validation
規(guī)則
和數(shù)據(jù)庫相關:
exists unique 'groups' => 'required|exists:groups,jid,parent,!0' // 表示 not exists
特殊使用:
after before // 日期相關
dimensions // 圖片相關 'avatar' => 'dimensions:min_width=100,min_height=200'
image // 表示這個文件必須是一個圖片
json // 必須是一個 JSON 字符串
accepted // true false 0 1 專門用在是否接受條款的按鈕上
active_url // 必須是一個有效的符合 checkdnsrr() 的地址
alpha // 必須是字母
alpha_dash // 必須是字母 數(shù)字 破折號 下劃線
alpha_num // 必須是字母 數(shù)字
digits // 一個指定長度的數(shù)字
digits_between // 某個長度之間的數(shù)字
integer // 必須是數(shù)字
numeric // 必須是數(shù)字或者數(shù)字字符串
boolean // ture false 1 0 '1' '0'
string // 必須是字符串
array // 必須是數(shù)組
distinct // 必須是不含有重復值的數(shù)組
between // 必須是區(qū)間之內(nèi)的數(shù)字
confirmed // 必須有一個 ***_confirmation 這樣的 field 存在
date // 必須是某個日期
date_format // 必須符合某種日期的格式
different // 必須不同于某個值
email // 必須是 email 格式的
file // 必須是一個上傳的文件
filled // 不能為空
in // 必須是一個 lists 中的一個
not_in // 必須不是一個 lists 中的一個
in_array // 必須是另一個字段中的一個值
ip // 必須是 IP 地址
max // 必須小于這個值
min // 必須大于某個值
mimetypes // 必須是某個文件類型
mimes // 必須帶有某個擴展名
nullable // 可以是 null
present // 必須在 input 中出現(xiàn)良风,但是可以為空
regex // 必須符合某個正則表達式
required // 必須要填寫
required_if // 如果某個別的字段有值,則不能為空
required_unless // 如果別的某個字段的值為某個值,則不能為空
required_with // 在某些別的字段有值的時候,不能為空
required_with_all // 在某些別的字段都有值的時候粟关,不能為空
required_without // 某些別的字段沒有值的時候,不能為空
required_without_all // 某些別的字段都沒有值的時候环戈,不能為空
same // 必須與某個別的字段的值一樣
size // 數(shù)字 字符串 文件 必須在某個區(qū)間內(nèi)
timezone // 必須是某個時區(qū)
url // 必須是 Url 地址
自定義 validation
Validator::extend() // 新創(chuàng)建
Validator::replacer() // 替換原有
Validator::extendImplicit() // 在值為null的情況下依然進行規(guī)則驗證
這塊等真正的運用場景出現(xiàn)的時候闷板,再補充
錯誤信息的封裝
將 Controller.php 文件修改為:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected function formatValidationErrors(Validator $validator)
{
if ($validator->fails()) {
// 假設所有 postman 的請求都是 AJAX 請求
if (request()->headers->get('postman-token'))
{
request()->headers->set('X-Requested-With', 'XMLHttpRequest');
}
return ['status'=>'fail', 'data' => $validator->errors()->all()];
}
}
}
sometimes 方法
這個方法可以對某些符合條件的表單進行判斷,非常的方便:
$validator->sometimes(['reason', 'cost'], 'required', function ($input) {
return $input->games >= 100;
});
多個數(shù)據(jù)同時驗證
$this->validate($request, [
'courseId' => "unique:teacher_student,course_id,NULL,id,teacher_id,{$teacherId},student_id,{$studentId}",
]);
// modify from - https://brianretterer.com/quick-tip-laravel-unique-validation/
驗證后返回使用
這是一個 5.5 版本的功能院塞,非常有用:
$validatedValue = $request->validate([
'continue' => 'sometimes|required|url',
]);
return view('auth.login', ['validatedValue'=>$validatedValue]);