可用驗(yàn)證規(guī)則

2018-02-24 15:52 更新

以下是現(xiàn)有可用的驗(yàn)證規(guī)則清單與他們的函數(shù)名稱:


  • Accepted
  • Active URL
  • After (Date)
  • Alpha
  • Alpha Dash
  • Alpha Numeric
  • Array
  • Before (Date)
  • Between
  • Boolean
  • Confirmed
  • Date
  • Date Format
  • Different
  • Digits
  • Digits Between
  • E-Mail
  • Exists (Database)
  • Image (File)
  • In
  • Integer
  • IP Address
  • Max
  • MIME Types
  • Min
  • Not In
  • Numeric
  • Regular Expression
  • Required
  • Required If
  • Required With
  • Required With All
  • Required Without
  • Required Without All
  • Same
  • Size
  • String
  • Timezone
  • Unique (Database)
  • URL

accepted

字段值為 yes, on, 或是 1 時(shí),驗(yàn)證才會(huì)通過(guò)。這在確認(rèn)"服務(wù)條款"是否同意時(shí)很有用。

active_url

字段值通過(guò) PHP 函數(shù) checkdnsrr 來(lái)驗(yàn)證是否為一個(gè)有效的網(wǎng)址。

after:date

驗(yàn)證字段是否是在指定日期之后。這個(gè)日期將會(huì)使用 PHP strtotime 函數(shù)驗(yàn)證。

alpha

字段僅全數(shù)為字母字串時(shí)通過(guò)驗(yàn)證。

alpha_dash

字段值僅允許字母、數(shù)字、破折號(hào)(-)以及底線(_)

alpha_num

字段值僅允許字母、數(shù)字

array

字段值僅允許為數(shù)組

before:date

驗(yàn)證字段是否是在指定日期之前。這個(gè)日期將會(huì)使用 PHP strtotime 函數(shù)驗(yàn)證。

between:min,max

字段值需介于指定的 min 和 max 值之間。字串、數(shù)值或是文件都是用同樣的方式來(lái)進(jìn)行驗(yàn)證。

confirmed

字段值需與對(duì)應(yīng)的字段值 foo_confirmation 相同。例如,如果驗(yàn)證的字段是 password ,那對(duì)應(yīng)的字段 password_confirmation 就必須存在且與 password 字段相符。

date

字段值通過(guò) PHP strtotime 函數(shù)驗(yàn)證是否為一個(gè)合法的日期。

date_format:format

字段值通過(guò) PHP date_parse_from_format 函數(shù)驗(yàn)證符合 format 制定格式的日期是否為合法日期。

different:field

字段值需與指定的字段 field 值不同。

digits:value

字段值需為數(shù)字且長(zhǎng)度需為 value。

digits_between:min,max

字段值需為數(shù)字,且長(zhǎng)度需介于 min 與 max 之間。

boolean

字段必須可以轉(zhuǎn)換成布爾值,可接受的值為 true, false, 1, 0, "1", "0"。

email

字段值需符合 email 格式。

exists:table,column

字段值需與存在于數(shù)據(jù)庫(kù) table 中的 column 字段值其一相同。
Exists 規(guī)則的基本使用方法

'state' => 'exists:states'

指定一個(gè)自定義的字段名稱

'state' => 'exists:states,abbreviation'

您可以指定更多條件且那些條件將會(huì)被新增至 "where" 查詢里:

'email' => 'exists:staff,email,account_id,1'
/* 這個(gè)驗(yàn)證規(guī)則為 email 需存在于 staff 這個(gè)數(shù)據(jù)庫(kù)表中 email 字段中且 account_id=1 */

通過(guò)NULL搭配"where"的縮寫(xiě)寫(xiě)法去檢查數(shù)據(jù)庫(kù)的是否為NULL

'email' => 'exists:staff,email,deleted_at,NULL'

image

文件必需為圖片(jpeg, png, bmp, gif 或 svg)

in:foo,bar,...

字段值需符合事先給予的清單的其中一個(gè)值

integer

字段值需為一個(gè)整數(shù)值

ip

字段值需符合 IP 位址格式。

max:value

字段值需小于等于 value。字串、數(shù)字和文件則是判斷 size 大小。

mimes:foo,bar,...

文件的 MIME 類需在給定清單中的列表中才能通過(guò)驗(yàn)證。
MIME規(guī)則基本用法

'photo' => 'mimes:jpeg,bmp,png'

min:value

字段值需大于等于 value。字串、數(shù)字和文件則是判斷 size 大小。

not_in:foo,bar,...

字段值不得為給定清單中其一。

numeric

字段值需為數(shù)字。

regex:pattern

字段值需符合給定的正規(guī)表示式。

注意: 當(dāng)使用regex模式時(shí),您必須使用數(shù)組來(lái)取代"|"作為分隔,尤其是當(dāng)正規(guī)表示式中含有"|"字串。

required

字段值為必填。

required_if:field,value

字段值在 field 字段值為 value 時(shí)為必填。

required_with:foo,bar,...

字段值 僅在 任一指定字段有值情況下為必填。

required_with_all:foo,bar,...

字段值 僅在 所有指定字段皆有值情況下為必填。

required_without:foo,bar,...

字段值 僅在 任一指定字段沒(méi)有值情況下為必填。

required_without_all:foo,bar,...

字段值 僅在 所有指定字段皆沒(méi)有值情況下為必填。

same:field

字段值需與指定字段 field 等值。

size:value

字段值的尺寸需符合給定 value 值。對(duì)于字串來(lái)說(shuō),value 為需符合的字串長(zhǎng)度。對(duì)于數(shù)字來(lái)說(shuō),value 為需符合的整數(shù)值。對(duì)于文件來(lái)說(shuō),value 為需符合的文件大?。▎挝?kb)。

timezone

字段值通過(guò) PHP timezone_identifiers_list 函數(shù)來(lái)驗(yàn)證是否為有效的時(shí)區(qū)。

unique:table,column,except,idColumn

字段值在給定的數(shù)據(jù)庫(kù)中需為唯一值。如果 column(字段) 選項(xiàng)沒(méi)有指定,將會(huì)使用字段名稱。

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)規(guī)則的基本用法

'email' => 'unique:users'

指定一個(gè)自定義的字段名稱

'email' => 'unique:users,email_address'

強(qiáng)制唯一規(guī)則忽略指定的 ID

'email' => 'unique:users,email_address,10'

增加額外的 Where 條件

您也可以指定更多的條件式到 "where" 查詢語(yǔ)句中:

'email' => 'unique:users,email_address,NULL,id,account_id,1'

上述規(guī)則為只有 account_id 為 1 的數(shù)據(jù)列會(huì)做唯一規(guī)則的驗(yàn)證。

url

字段值需符合 URL 的格式。

注意: 此函數(shù)會(huì)使用 PHP filter_var 方法驗(yàn)證。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)