濮阳杆衣贸易有限公司

主頁 > 知識庫 > laravel 數據驗證規(guī)則詳解

laravel 數據驗證規(guī)則詳解

熱門標簽:江門回撥外呼系統(tǒng) 天潤融通外呼系統(tǒng)好嗎 高德地圖標注位置怎么標注 高德地圖標注店鋪收費嗎 欣思維地圖標注 江西電銷機器人收費 泊頭在哪里辦理400電話 杭州語音電銷機器人 電銷機器人沒有效果怎么樣

如下所示:

return [
  'accepted' => '必須為yes,on,1,true',
  'active_url' => '是否是一個合法的url,基于PHP的checkdnsrr函數,因此也可以用來驗證郵箱地址是否存在',
  'after:date' => '驗證字段必須是給定日期后的值,比如required|date|after:tomorrow,通過PHP函數strtotime來驗證',
  'after_or_equal:date' => '大于等于',
  'alpha' => '驗證字段必須全是字母',
  'alpha_dash' => '驗證字段可能具有字母、數字、破折號、下劃線',
  'alpha_num' => '驗證字段必須全是字母和數字',
  'array' => '數組',
  'before:date' => '小于',
  'before_or_equal:date' => '小于等于',
  'between:min,max' => '給定大小在min,max之間,字符串,數字,數組或者文件大小都用size函數評估',
  'boolean' => '必須為能轉化為布爾值的參數,比如:true,false,1,0,"1","0"',
  'confirmed' => '字段必須與foo_confirmation字段值一致,比如,要驗證的是password,輸入中必須存在匹配的password_confirmation字段',
  'date' => '通過strtotime校驗的有效日期',
  'date_equals:date' => '等于',
  'date_format:format' => 'date和date_format不應該同時使用,按指定時間格式傳值',
  'different:field' => '驗證的字段值必須與字段field的值相同',
  'digits:value' => '必須是數字,并且有確切的值',
  'digits_between:min,max' => '字段長度必須在min,max之間',
  'dimensions' => '驗證的文件是圖片并且圖片比例必須符合規(guī)則,比如dimensions:min_width=100,min_height=200,可用
          的規(guī)則有min_width,max_width,min_height,max_height,width,height,ratio',
  'distinct' => '無重復值',
  'email' => '符合e-mail地址格式',
  'exists:table,column' => '必須存在于指定的數據庫表中',
  'file' => '成功上傳的文件',
  'filled' => '驗證的字段存在時不能為空',
  'image' => '驗證的文件必須是圖像,jpeg,png,bmp,gif,svg',
  'in:foo,bar,...' => '驗證的字段必須包含在給定的值列表中',
  'in_array:anotherfield' => '驗證的字段必須存在于另一個字段的值中',
  'integer' => '整數',
  'ip' => 'ip地址',
  'ipv4' => 'ipv4地址',
  'ipv6' => 'ipv6地址',
  'json' => 'json字符串',
  'max:value' => '大于',
  'mimetypes:text/plain,...' => '驗證的文件必須與給定的MIME類型匹配',
  'mimes:foo,bar,...' => '驗證的文件必須具有列出的其中一個擴展名對應的MIME類型',
  'min:value' => '小于',
  'nullable' => '可為null,可以包含空值的字符串和整數',
  'not_in:foo,bar...' => '不包含',
  'numeric' => '必須為數字',
  'present' => '驗證的字段必須存在于輸入數據中,但可以為空',
  'regex:pattern' => '驗證的字段必須與給定正則表達式匹配',
  'required' => '驗證的字段必須存在于輸入數據中,但不可以為空',
          //以下情況視為空:1.該值為null,2.空字符串,3.空數組或空的可數對象,4.沒有路徑的上傳文件
  'required_if:anotherfield,value,...' => '如果指定的anotherfield等于value時,被驗證的字段必須存在且不為空',
  'required_unless:anotherfield,value,...' => '如果指定的anotherfield等于value時,被驗證的字段不必存在',
  'required_with:foo,bar,...' => '只要指定的其它字段中有任意一個字段存在,被驗證的字段就必須存在且不為空',
  'required_with_all:foo,bar,...' => '當指定的其它字段必須全部存在時,被驗證的字段才必須存在且不為空',
  'required_without_all:foo,bar,...' => '當指定的其它字段必須全部不存在時,被驗證的字段必須存在且不為空',
  'required_without:foo,bar,...' => '當指定的其它字段有一個字段不存在,被驗證的字段就必須存在且不為空',
  'same:field' => '給定字段必須與驗證字段匹配',
  'size:value' => '驗證字段必須具有與給定值匹配的大小,對字符串,value對應字符數;對數字,對應給定的
          整數值;對數組,對應count值;對文件,是文件大?。╧b)',
  'timezone' => '驗證字段是有效的時區(qū)標識符,根據PHP函數timezone_identifiers_list判斷',
  'unique:table,column,except,idColumn' => '驗證字段必須是數據庫中唯一的',
  'url' => '有效的url',
];

簡單例子

return [
  'title.required' => 'A title is required',
  'body.required' => 'A message is required',
  'avatar' => [
    'required',
    Rule::dimensions()->maxWidth(500)->maxHeight(250)->ratio(3/2), //限制圖片大小和比例
  ],
  'foo.*.id' =>'distinct', //不允許重復
  'state' =>'exists:states', //指定表
  'state1' => 'exists:states,abbreviation', //指定表和字段
  'email' => 'exists:connection.staff,email', //指定查詢的數據庫
  'email1' => [
    'required',
    Rule::exists('staff')->where(function ($query){
      $query->where('account_id',1);
    }),
  ],
  'zones' => [
    'required',
    Rule::in(['first-zone','second-zone']),
  ],
  'video' => 'mimetypes:video/avi,video/mpeg,video/quicktime',
  'photo' => 'mimes:jpeg,bmp,png', //驗證文件擴展名,規(guī)則上也會驗證文件的MIME類型,通過讀取文件的內容以猜測它的MIME類型
  'toppings' => [
    'required',
    Rule::notIn(['sprinkles','cherries']),
  ],
  //當使用regex時,必須使用數組,而不是|分隔符,特別是正則中有|時
  'email2' => 'unique:users,email_address',
  'email3' => 'unique:connection.users,email_address', //指定數據庫
  'email4' => Rule::unique('users')->where(function ($query){
    $query->where('account_id',1);
  }),
  'custom' => [
    'person.*.email' => [
      'unique' => 'each person must have a unique e-mail address',
    ]
  ],
];

特殊例子

//驗證時忽視id
Validator::make($data,[
  'email' => [
    'required',
    Rule::unique('users')->ignore($user->id,'user_id'),
  ]
]);

//在某些情況下,只有在該字段存在于輸入數組中時,才可以對字段執(zhí)行驗證檢查
$v = Validator::make($data,[
  'email' => 'sometimes|required|email',//email只有在data數組中時才會被驗證
]);

$z = Validator::make($data,[
  'email' => 'required|email',
  'games' => 'required|numeric',
]);
$z->sometimes('reason','required|max:500',function ($input){
  return $input->games >= 100; //當值超過100時,reson才必填
});
$z->sometimes(['reson','cost'],'required',function ($input){
  return $input->games >= 100;
});
$validator = Validator::make($request->all(),[
  'photos.profile' => 'required|image',//驗證數組中的某個key的值
]);

$validator = Validator::make($request->all(),[
  'person.*.email' => 'email|unique:users',
  'person.*.first_name' => 'required_with:person.*.last_name',
]);//驗證指定數組輸入字段中的每一個email都是唯一的

$request->validate([
  'name' => ['required', new Uppercase()],
]);
$validator = Validator::make($this->request,[
  'title' => 'required|unique:posts|max:255',
  'body' => 'required',
])->validate();

$validator->after(function ($validator){
  if ($this->somethingElseIsInvalid()) {
    $validator->errors()->add('field', 'Something is wrong with this field!');
  }
});

if ($validator->fails()){

}

$errors = $validator->errors();
echo $errors->first('email');

//以數組形式獲取指定字段的所有錯誤消息
foreach ($errors->get('email') as $message){

}

//驗證表單的數組字段,獲取數組元素的所欲錯誤消息
foreach ($errors->get('attachments.*') as $message){

}

//查看所有字段的錯誤消息
foreach ($errors->all() as $message){

}

// 檢測一個字段是否有錯誤消息
if ($errors->has('email')){

}

以上這篇laravel 數據驗證規(guī)則詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • laravel 表單驗證實現多個字段組合后唯一
  • Laravel 驗證碼認證學習記錄小結
  • Laravel實現登錄跳轉功能
  • laravel 解決強制跳轉 https的問題
  • Laravel重定向,a鏈接跳轉,控制器跳轉示例
  • 解決Laravel使用驗證時跳轉到首頁的問題

標簽:平涼 內江 雙鴨山 江門 石嘴山 大同 駐馬店 深圳

巨人網絡通訊聲明:本文標題《laravel 數據驗證規(guī)則詳解》,本文關鍵詞  laravel,數據,驗證,規(guī)則,詳解,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《laravel 數據驗證規(guī)則詳解》相關的同類信息!
  • 本頁收集關于laravel 數據驗證規(guī)則詳解的相關信息資訊供網民參考!
  • 推薦文章
    灌阳县| 祁门县| 瓮安县| 绩溪县| 澄江县| 丽水市| 通辽市| 嘉定区| 清苑县| 花莲市| 玉树县| 长沙县| 崇明县| 高台县| 大理市| 阳谷县| 万州区| 柯坪县| 墨竹工卡县| 光泽县| 寿光市| 客服| 南阳市| 安宁市| 通榆县| 沭阳县| 于田县| 宁晋县| 鱼台县| 疏勒县| 柳江县| 牡丹江市| 六盘水市| 恭城| 望江县| 上杭县| 宁阳县| 天峻县| 綦江县| 旌德县| 长岛县|