namespace App\Http\Controllers;
use App\Models\Users;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\Request;
class UsersController extends Controller{
// 添加用戶post數據驗證
public function create(Request $request){
if($request->isMethod('POST')){
$data=$request->input('users');
// 1.控制器的驗證演示 Controller中 use ValidatesRequests 就可以直接調用;
// blade中HTML中form表單中name的傳值演示 <input name="users[u_mobile]" placeholder="請輸入" class="layui-input" type="text">
// <input name="u_gender" value="1" type="radio"><input name="u_gender" value="2" type="radio">
$this->validate($request,[ // 以下正則驗證都是自己隨意編寫的並沒有嚴格去驗證,如需要嚴格的請自行編寫或百度,#此處只為重點講Laravel可用驗證用法
//required必須輸入 unique 唯一(在users表中u_mobile[手機號]為唯一,不允許重復,regex表達式驗證) 此處要用多個條件驗證和regex正則建議采用[]形式中間用逗號分隔
'users.u_mobile'=>['required','unique:users','regex:/^1[3|4|5|7|8][0-9]{9}$/'], // 如果是一維數組直接'u_mobile'就可以了;
// 編輯過程中驗證唯一示例如:'users.u_mobile'=>['required',Rule::unique('users')->ignore($user->id),'regex:/^1[3|4|5|7|8][0-9]{9}$/'],
// $user為查出的用戶對象,ignore是為了強迫Unique規則忽略指定ID($user->id)再進行判斷唯一,如果不是在控制器中使用validate驗證你可以采用request('id')來獲取指定id,request為一個輔助函數很實用
'users.u_qq'=>['required','regex:/^[1-9][0-9]{4,}$/'], // QQ號 還可采用簡寫 'users.u_qq' => 'required|regex:/^[1-9][0-9]{4,}$/',
'users.u_email'=>['required','unique:users','regex:/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/'], // 大概的郵箱驗證
'u_gender'=>'required|in:1,2', // in 的用法用戶性別只能輸入1和2(男/女) 此處采用中划線的形式驗證 如果文字也可以設置為 'required|in:男,女'
'users.u_nickname'=>['required','regex:/^[_\w\d\x{4e00}-\x{9fa5}]{3,20}$/iu'], // 隨意寫的用戶名驗證
/*****以下開始重點演示可用的驗證用法,有些命名屬性和users幾乎無什么關聯了,重點看驗證規則寫法,其它的並不重要****/
'salary' => 'required|integer|min:1000', // 工資 integer必須為整數,但是數字必須不能超過2147483647否則它將判斷為“不是整數”, min最小值為1000
// nullable 允許輸入為空, required_if如果用戶性別選擇了為1那么input中屬性name="name"輸入框就必須要填寫
'users.name' => 'nullable|required_if:u_gender,1|string|between:1,10', // between 之間(包括1和10) 姓名必須在字數1位和10位之間
'tel'=>['nullable','regex:/^((0\d{2,3}-?)?\d{7,8})|(1[3-9]\d{9})$/'], // 聯系電話(座機號和手機號都可以),輸入可以為空,但有輸入值時就必須按照正則驗證
'users.id_no' =>['required','regex:/(^\d{17}[0-9xX]{1}$)|(^\d{14}[0-9xX]{1}$)/'], // 身份證號碼驗證(包括15位的驗證)
'users.age' => ['nullable', 'integer', 'regex:/^[1-9][0-9]?$/'], //年齡 也可以嘗試中划線形式:'nullable|integer|regex:/^[1-9][0-9]?$/'
// 余額 numeric必須為數字,max最大值為922337203685477 但是max設置為9223372036854771 但用戶輸入9223372036854772、9223372036854773 也可以驗證通過
// max設置為9223372036854775807 但輸入 9223372036854775808、9223372036854775809 還是會通過 所以max還有些缺陷請謹慎使用!
// 同樣的問題在 between用於數字類型的精確的范圍之間驗證也會出現問題,如:between:1,9223372036854771 輸入9223372036854773 也會通過,也需要注意!!!
'balance' => 'nullable|numeric|min:1|max:922337203685477', //max如果是數字類型,最大為922337203685477內能精確驗證,超過了可能出現問題.
//html模版如: 互聯網<input type="checkbox" name="industry_nos[]" value="1"> 建築<input type="checkbox" name="industry_nos[]" value="2">
'industry_nos' =>'required|array', // 行業 array 驗證的字段必須是一個php數組
'industry_nos.*' =>'integer|distinct|between:1,6', // 整數必須在1和6之間 distinct 指定的字段不能有任何重復值
'telephone' => ['required_without:id_no', 'regex:/^1[3-9]\d{9}$/'], // 手機號 required_without 只要id_no字段不存在,telephone那么就必須存在且不能為空
'id_no' => ['required_without:telephone', 'regex:/(^\d{17}[0-9xX]{1}$)|(^\d{14}[0-9xX]{1}$)/'], // 身份證號碼 只要telephone不存在,那么id_no就必須存在不能為空
//html模版如: 密碼二次確認 <input type="password" name="password" /> <input type="password" name="password_confirmation" />
'password'=>'required|min:6|confirmed', // confirmed 驗證的字段必須和password_confirmation 保持一樣否則就會驗證不成功
'password_confirmation' => 'required|min:6', // password_confirmation會驗證和confirmed一致
'pics' =>'required|json|not_in:[]', // json必須為有效的json字符串,not_in不能為包含給定的值[]
'birthday'=>'nullable|date', // 驗證的字段值必須是通過PHP函數strtotime校驗的有效日期
'start_date' => 'nullable|date_format:Y-m-d', //開始時間 date_format 驗證的字段必須與給定的時間格式相匹配
'amount' => ['required','numeric','regex:/^\d{1,14}$/'], // 正則允許的最大為: 99999999999999,超過即為不合法
//msyql bigint帶符號的最大值為9223372036854775807,但php中如果位數超過14將采用科學計數法如果采用max不太合適,所以先默認使用上面的正則驗證方法
],[
'required'=>':attribute為必填項',//:attribute 字段占位符表示字段名稱
'regex'=>':attribute格式不正確',
'unique'=>':attribute已經存在',
'integer'=>':attribute必須為整數',
'min'=>':attribute不能小於6位',
'email'=>':attribute格式不正確',
'in'=>':attribute必須選擇',
'max'=>':attribute大於了10位',
// 此處開始省略..就不再繼續編寫了,寫法同上面都差不多,自行編寫即可
],[
'users.u_mobile'=>'手機號碼', //指定字段名稱
'users.u_qq'=>'QQ號碼',
'users.u_email'=>'聯系郵箱',
'u_gender'=>'用戶性別',
'users.u_nickname'=>'用戶昵稱',
// 此處開始省略..就不再繼續編寫了,寫法同上面都差不多,自行編寫即可
]);
// 2. validator類驗證演示---------------
/*$validator=Validator::make($request->input(),[
'users.u_mobile'=>['required','unique:users','regex:/^1[3|4|5|7|8][0-9]{9}$/'],//如果是一維數組直接'u_mobile'就可以了;
'users.u_qq'=>['required','regex:/^[1-9][0-9]{4,}$/'],
'users.u_email'=>'required|unique:users|email',
'users.u_gender'=>'required|in:0,1,2',
'users.u_nickname'=>['required','regex:/^[_\w\d\x{4e00}-\x{9fa5}]{3,20}$/iu'],
],[
'required'=>':attribute為必填項',//:attribute 字段占位符表示字段名稱
'regex'=>':attribute格式不正確',
'unique'=>':attribute已經存在',
'integer'=>':attribute必須為整數',
'min'=>':attribute不能小於6位',
'email'=>':attribute格式不正確',
'in'=>':attribute必須選擇',
'max'=>':attribute大於了10位',
],[
'users.u_mobile'=>'手機號碼', //指定字段名稱
'users.u_qq'=>'QQ號碼',
'users.u_email'=>'聯系郵箱',
'users.u_gender'=>'用戶性別',
'users.u_nickname'=>'用戶昵稱',
]);
if($validator->fails()){
abort(422, $validator->errors()->first());
}*/
}
}
}
如果需要參考框架中驗證類的源碼可以進入 \vendor\laravel\framework\src\Illuminate\Validation\Concerns\ValidatesAttributes.php 文件中進行詳細查看流程和調試,可以多研究一下
