accepted
字段值為 yes, on, 或是 1 時,驗證才會通過。這在確認"服務條款"是否同意時很有用。
active_url
字段值通過 PHP 函數 checkdnsrr 來驗證是否為一個有效的網址。
after:date
驗證字段是否是在指定日期之后。這個日期將會使用 PHP strtotime 函數驗證。
alpha
字段僅全數為字母字串時通過驗證。
alpha_dash
字段值僅允許字母、數字、破折號(-)以及底線(_)
alpha_num
字段值僅允許字母、數字
array
字段值僅允許為數組
before:date
驗證字段是否是在指定日期之前。這個日期將會使用 PHP strtotime 函數驗證。
between:min,max
字段值需介於指定的 min 和 max 值之間。字串、數值或是文件都是用同樣的方式來進行驗證。
confirmed
字段值需與對應的字段值 foo_confirmation 相同。例如,如果驗證的字段是 password ,那對應的字段 password_confirmation 就必須存在且與 password 字段相符。
date
字段值通過 PHP strtotime 函數驗證是否為一個合法的日期。
date_format:format
字段值通過 PHP date_parse_from_format 函數驗證符合 format 制定格式的日期是否為合法日期。
different:field
字段值需與指定的字段 field 值不同。
digits:value
字段值需為數字且長度需為 value。
digits_between:min,max
字段值需為數字,且長度需介於 min 與 max 之間。
boolean
字段必須可以轉換成布爾值,可接受的值為 true, false, 1, 0, "1", "0"。
字段值需符合 email 格式。
exists:table,column
字段值需與存在於數據庫 table 中的 column 字段值其一相同。
Exists 規則的基本使用方法
'state' => 'exists:states'
指定一個自定義的字段名稱
'state' => 'exists:states,abbreviation'
您可以指定更多條件且那些條件將會被新增至 "where" 查詢里:
'email' => 'exists:staff,email,account_id,1'
/* 這個驗證規則為 email 需存在於 staff 這個數據庫表中 email 字段中且 account_id=1 */
通過NULL搭配"where"的縮寫寫法去檢查數據庫的是否為NULL
'email' => 'exists:staff,email,deleted_at,NULL'
image
文件必需為圖片(jpeg, png, bmp, gif 或 svg)
in:foo,bar,...
字段值需符合事先給予的清單的其中一個值
integer
字段值需為一個整數值
ip
字段值需符合 IP 位址格式。
max:value
字段值需小於等於 value。字串、數字和文件則是判斷 size 大小。
mimes:foo,bar,...
文件的 MIME 類需在給定清單中的列表中才能通過驗證。
MIME規則基本用法
'photo' => 'mimes:jpeg,bmp,png'
min:value
字段值需大於等於 value。字串、數字和文件則是判斷 size 大小。
not_in:foo,bar,...
字段值不得為給定清單中其一。
numeric
字段值需為數字。
regex:pattern
字段值需符合給定的正規表示式。
注意: 當使用regex模式時,您必須使用數組來取代"|"作為分隔,尤其是當正規表示式中含有"|"字串。
required
字段值為必填。
required_if:field,value
字段值在 field 字段值為 value 時為必填。
required_with:foo,bar,...
字段值 僅在 任一指定字段有值情況下為必填。
required_with_all:foo,bar,...
字段值 僅在 所有指定字段皆有值情況下為必填。
required_without:foo,bar,...
字段值 僅在 任一指定字段沒有值情況下為必填。
required_without_all:foo,bar,...
字段值 僅在 所有指定字段皆沒有值情況下為必填。
same:field
字段值需與指定字段 field 等值。
size:value
字段值的尺寸需符合給定 value 值。對於字串來說,value 為需符合的字串長度。對於數字來說,value 為需符合的整數值。對於文件來說,value 為需符合的文件大小(單位 kb)。
timezone
字段值通過 PHP timezone_identifiers_list 函數來驗證是否為有效的時區。
unique:table,column,except,idColumn
字段值在給定的數據庫中需為唯一值。如果 column(字段) 選項沒有指定,將會使用字段名稱。
Occasionally, you may need to set a custom connection for database queries made by the Validator. As seen above, setting unique:users as a validation rule will use the default database connection to query the database. To override this, do the following:
$verifier = App::make('validation.presence');
$verifier->setConnection('connectionName');
$validator = Validator::make($input, [
'name' => 'required',
'password' => 'required|min:8',
'email' => 'required|email|unique:users',
]);
$validator->setPresenceVerifier($verifier);
唯一(Unique)規則的基本用法
'email' => 'unique:users'
指定一個自定義的字段名稱
'email' => 'unique:users,email_address'
強制唯一規則忽略指定的 ID
'email' => 'unique:users,email_address,10'
增加額外的 Where 條件
您也可以指定更多的條件式到 "where" 查詢語句中:
'email' => 'unique:users,email_address,NULL,id,account_id,1'
上述規則為只有 account_id 為 1 的數據列會做唯一規則的驗證。
url
字段值需符合 URL 的格式。
注意: 此函數會使用 PHP filter_var 方法驗證。
