規則
accepted
該字段的值必須是yes、on、1或true.必填規則
'is_read'=>'accepted'
active_url
必須是一個基於PHP函數checkdnsrr 的有效URL,
可以認爲是存在的一個域名地址
'url'=>'active_url'
after:date
給定日期之後的日期
//驗證日期必須是名字之後的日期,比如你的驗證值是2015-05-25 10:23:15
'start_date' => 'required|date|after:tomorrow'
//驗證2018-05-26之後日期
'start_date'=>''after:2018-05-26''
before:date
驗證字段必須是指定日期之前的一個數值
//驗證日期必須是名字之前的日期,比如你的驗證值是2015-05-25 10:23:15
'start_date' => 'required|date|after:tomorrow'
//驗證2018-05-26之前日期
'start_date'=>''after:2018-05-26''
alpha,
該字段必須是字母,類似正則[a-zA-Z]
'ename'=>'alpha'
alpha_dash
該字段可以包含字母和數字,以及破折號和下劃線,類似正則\w
alpha_num
該字段必須是字母或數字
array
該字段必須是PHP數組
between:min,max
驗證字段尺寸在給定的最小值和最大值之間,字符串、數值和文件都可以使用該規則
表示字符長度,中文也按字數算
'name'=>'between:10,100|required'
boolean
字段必須可以被轉化爲boolean,接收true, false, 1,0, "1", 和 "0"等輸入
confirmed
驗證字段必須有一個匹配字段,比如確認密碼之類
需要2個表單名字,例如一個叫name,另外一個必須是name_confirmation
‘name’=>'confirmed'
date
驗證有效的日期。
//2018-05-24 11:40:26驗證通過,
‘age_date’=>'date'
date_format:format
驗證字段必須匹配指定格式,
//2018-05-24 驗證通過,2018-05-24 18:0:0 驗證不通過
‘age_date’=>'date_format:Y-m-d'
different:field
驗證字段必須是一個和指定字段不同的值,跟這個confirmed相反
digits:value
必須是數字且長度爲value指定的值
'age'=>'digits:2'
digits_between:min,max
驗證字段數值長度必須介於最小值和最大值之間,必須是數字
dimensions
驗證的圖片尺寸必須滿足該規定參數指定的約束條件:
distinct
處理數組時,驗證字段不能包含重複值:
'foo.*.id' => 'distinct'
驗證字段必須是格式化的電子郵件地址
exists:table,column
驗證字段的值必須存在於數據表
//驗證數據表users的name字段是否存在你給的值,如果存在則通過
'name' => 'exists:users,name'
條件到where查詢子句
'email' => 'exists:staff,email,account_id,1'
'email' => 'exists:staff,email,role,!admin'
'email' => 'exists:staff,email,deleted_at,NULL'
'email' => 'exists:staff,email,deleted_at,NOT_NULL'
後面2個參數是條件,deleted_at is null
file
該驗證字段必須是上傳成功的文件
filled
該驗證字段如果存在則不能爲空
image
驗證文件必須是圖片(jpeg、png、bmp、gif或者svg)
in:foo,bar…
驗證字段值必須在給定的列表中
'name'=>'in:kongqi,bst',
not_in:foo,bar,…
驗證字段值不在給定列表中
in_array
驗證字段必須在另一個字段中存在
'name'=>'in_array:name_confirmation',
integer
驗證字段必須是整型
ip
驗證字段必須是IP地址
JSON
驗證字段必須是有效的JSON字符串
size:value
驗證字段必須有和給定值value相匹配的尺寸,對字符串而言,value是相應的字符數目;對數值而言,value是給定整型值;對文件而言,value是相應的文件字節數
max:value
驗證字段必須小於等於最大值,和字符串、數值、文件字段的size規則一起使用
min:value
驗證字段的最小值,對字符串、數值、文件字段而言,和size規則使用方式一致。
mimetypes:text/plain…
string
驗證字段必須是字符串
unique:table,column,except(忽略的字段值),idColumn(默認忽略是ID字段)
驗證字段在給定數據表上必須是唯一的,如果不指定column選項,字段名將作爲默認column。
指定自定義列名:
'email' => 'unique:users,email_address'
自定義數據庫連接
'email' => 'unique:connection.users,email_address'
強制一個唯一規則來忽略給定ID:
'email' => 'unique:users,email_address,'.$user->id
//如果你的數據表使用主鍵字段不是id,可以指定第四個輸入參數:
'email' => 'unique:users,email_address,'.$user->id.',user_id'
//添加額外的where子句:
//只有account_id爲1記錄纔會進行唯一性檢查。
'email' => 'unique:users,email_address,NULL,id,account_id,1'
numeric
驗證字段必須是數值
string
驗證字段必須是字符串
present
驗證字段必須出現在輸入數據中但可以爲空。
regex:pattern
驗證字段必須匹配給定正則表達式
規則必須放在數組中
'name'=>[
'regex:/\d/',
'required',
],
required
輸入字段值不能爲空,以下情況字段值都爲空:
值爲null
值是空字符串
值是空數組或者空的Coutable對象
值是上傳文件但路徑爲空
required_if:anotherfield,value,…
驗證字段在另一個字段等於指定值value時是必須的
'name'=>[
//假如name_confirmation的值=2就name就必填
'required_if:name_confirmation,2',
],
required_unless:anotherfield,value,…
除了 anotherfield 字段等於value,驗證字段不能空
'name'=>[
//假如name_confirmation的值!=2就name就必填
'required_unless:name_confirmation,2',
],
required_with:foo,bar,…
驗證字段只有在任一其它指定字段存在的話纔是必須的
'name'=>[
//如果其他name_confirmation表單字段存在值,則name需要必填
'required_with:name_confirmation'
],
required_with_all:foo,bar,…
驗證字段只有在所有指定字段存在的情況下才是必須的
required_without:foo,bar,…
驗證字段只有當任一指定字段不存在的情況下才是必須的
required_without_all:foo,bar,…
驗證字段只有當所有指定字段不存在的情況下才是必須的
自定義驗證規則
providers註冊
app/Providers/AppServiceProvider.php
//引入命名
use Validator;
public function boot()
{
Validator::extend('foo', function($attribute, $value, $parameters, $validator) {
return $value == 'foo';
});
}
$attribute 屬性名字, $value值,$parameters參數,$validator驗證實例
註冊類
public function boot()
{
Validator::extend('foo', 'FooValidator@validate');
}
定義錯誤信息
在語言包裏面的驗證文件的custom下數組定義,比如
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
'name'=>[
'alpha'=>' bi xi shi zimu',
"foo" => "Your input was invalid!",
],
],