開發的時候在驗證傳入參數方面,只有用到幾種常用規則像是 required、string、number 等等,沒有用到比較深入的規則想說整理一下並且多認識一些驗證規則(使用的 Laravel 版是是10) # 欄位顯示 ## exclude: 欄位會被排除掉當使用 validate 或 validated ## filled: 當出現這個欄位時,它不能是空值 ## missing: 不能出現欄位 ## present: 欄位必需要出現 ## prohibited: 欄位需要是 missing 或 empty empty 如果欄位是以下的值 - null - 空字串 - 空陣列 - 空物件 - 如果是上傳的檔案要是空路徑 ## required: 需要是 present 且不是 empty empty 是 null, 空字串, 空陣列, 空物件, 上傳檔案是空路徑 # 資料類型 ## array: 需要是 php 的 array ## boolean: 需要可以 cast 成 boolean ex: true, false, 1, 0, "1", "0" ## enum: 必需要是 valid 的 enum 值 ## nullable: 欄位可以是 null # 特殊資料類型 ## alpha: 必需是 Unicode alphabetic characters( \p{L}, \p{M}),如果要限制是ascii ``` 'username' => 'alpha:ascii', ``` ## alpha_dash: 必需是 Unicode alpha-numeric characters(\p{L}, \p{M}, \p{N}) 包含 - 與 _ ## alpha_num: 必需是 Unicode alpha-numeric characters(\p{L}, \p{M}, and \p{N}) ## ascii: 需要是 7-bit ASCII 字元 ## email: 必需要是 email ## hex_color: 需要包含 valid [16進位的顏色碼](https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color) ## ulid: 必需要是 ulid ## uuid: 必需要是 uuid # 數字 ## digits: 整數必須要有特定長度的值 ``` digits:value ``` ## decimal: 要是數字且具有特定小數位數 ``` // 最少要有.後兩位數 ex: 9.99 'price' => 'decimal:2' // 最少要有.後兩位數 到小數後四位數 'price' => 'decimal:2,4' ``` ## digits_between: 整數必需要在特定長度區間 ``` digits_between:min,max ``` ## integer: 必需要是整數 ## multiple_of: 必需要是某個值的倍數 ``` multiple_of:value ``` ## max_digits: 整數最多只能有 value 長度的值 ``` max_digits:value ``` ## min_digits: 整數最少要有最少 value 長度的值 ``` min_digits:value ``` ## numeric: 欄位需要是 numeric 是用 php 的 is_numeric 去驗證 # 字串 ## doesnt_start_with: 不可以從某個值開始 ``` doesnt_start_with:foo,bar,... ``` ## doesnt_end_with: 不可以從值結束 ``` doesnt_end_with:foo,bar,... ``` ## ends_with: 必需要是某個值的結尾 ``` ends_with:foo,bar,... ``` ## lowercase: 必需要是小寫 ## not_regex: 不能是等同指定的 regular expression ``` not_regex:pattern ``` ## regex: 要符合某個regular expression ``` regex:pattern ``` ## string: 要是字串 ## starts_with: 要從某個值開始 ``` starts_with:foo,bar,... ``` ## uppercase: 必需要是大寫 # 真假值 ## accepted: 欄位值必需是 "yes", "on", 1, "1", true, or "true" ## declined: 欄位值必需是 "no", "off", 0, "0", false, or "false" # 陣列 ## distinct: 在陣列內某個欄位值必需是唯一,不可以有重複 ``` 'foo.*.id' => 'distinct' // strict 'foo.*.id' => 'distinct:strict' // 忽略大小寫 'foo.*.id' => 'distinct:ignore_case' ``` ## in_array: 這個欄位值必需存在在另一個欄位裡 ``` in_array:anotherfield.* ``` ## in: 欄位值要包含在定義好的列表中 ```php Validator::make($data, [ 'zones' => [ 'required', Rule::in(['first-zone', 'second-zone']), ], ]); ``` ## not_in: 不能出現在列出的值中 ``` Validator::make($data, [ 'toppings' => [ 'required', Rule::notIn(['sprinkles', 'cherries']), ], ]); ``` # size 相關 ## size: 必需要符合value ``` size:value // string 要符合是幾個字 'title' => 'size:12'; // integer 要符合數值 'seats' => 'integer|size:10'; // array 符合有幾個元素 'tags' => 'array|size:5'; // file 符合檔案大小 'image' => 'file|size:512'; (512KB) ``` ## gt: 必需要大於某個欄位 ``` gt:field ``` ## gte: 必需要大於或等於某個欄位 ``` gte:field ``` ## lt: 要小於某個欄位 ``` lt:field ``` ## lte: 要小於等於某個欄位 ``` lt:field ``` ## max: 要小於或等於這個值 ``` max:value ``` ## min: 最少要是某個數值 ``` min:value ``` ## between: 在某個區間 ``` between:min,max ``` # 日期 ## after: 在某個日期之後,根據 strtotime php function ```php 'start_dat' => 'required|date|after:tomorrow' ``` ## after_or_equal: 同 after,只是多了可以日期相同 ## before: 在某個日期之前 ## before_or_equal: 在等於某個日期或之前 ## date: 必需要是 valid 日期且非相對的 ## date_equals: 日期必需要相等 ```php date_equals:date ``` ## date_format: 日期必需要是某個格式 ```php date_format:format,... ``` # 時區 - timezone: 要是某個時區 ``` 'timezone' => 'required|timezone:all'; 'timezone' => 'required|timezone:Africa'; 'timezone' => 'required|timezone:per_country,US'; ``` # 檔案上傳 ## dimensions: 必需要是圖片且具有特定的限制 ``` 'avatar' => 'dimensions:min_width=100,min_height=200' ``` ## extensions: 指定上傳的副檔名 ``` extensions:foo,bar,... // example 'photo' => ['required', 'extensions:jpg,png'], ``` ## file: 必需要是檔案 ## image: 上傳的檔案必需是圖片(jpg, jpeg, png, bmp, gif, svg, or webp) ## mimetypes: 必需要是某個 mime 類型 ``` mimetypes:text/plain,... ``` ## mimes: 必需要是列出的 [mime type](https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types) 中的一個 ``` 'photo' => 'mimes:jpg,bmp,png' ``` # IP ## ip: 必需要是 ip ## ipv4: 必需要是 ipv4 ## ipv6: 必需要是 ipv6 # 網址 ## active_url: 這個欄位必需要是 valid A 或 AAAA 紀錄 - 根據 dns_get_record 這個 php function 這裡的A代表DNS A的A,A代表位址,它表示給定網域的IP 位置. 例如拉取 cloudflare.com 的 DNS 記錄,A 記錄當前傳回的 IP 位址為:104.17.210.9,A紀錄只會保留 IPv4 位置,如果有 IPv6 則會用 AAAA 紀錄 [什麼是 DNS A 記錄? ](https://www.cloudflare.com/zh-tw/learning/dns/dns-records/dns-a-record/) ## mac_address: 必需要是 MAC address ``` 00:1A:2B:3C:4D:5E ``` ## url: 必需要是網址 # 資料庫 ## exists: 這個欄位的值需要出現在資料庫的欄位中 ``` exists:table,column //要指定特定的 connection 'email' => 'exists:connection.staff,email' ``` ## unique: 這個欄位值不能出現在資料庫的欄位中 ``` unique:table,column ``` # 其它 ## bail: 當第一個驗證錯誤時就中斷 ## confirmed: 必需要配對到 {field}_confirmation,且輸入的內容要一樣,filed 是 輸入的為名稱 ex: password 就必需要有一個 password_confirmation ## current_password: 必需要等同授權使用者的密碼 ## different: 需要和另一個欄位的值不同 ``` different:field ``` ## required_if_accepted: 當另一個欄位值為 accepted 時,要符合 required ``` required_if_accepted:anotherfield,... ``` ## required_without_all: 當所有其他欄位不為 required 時,這個欄位為 required ``` required_without_all:foo,bar,... ``` ## required_array_keys: 這個欄位要是 array 且至少包含一個列出key ``` required_array_keys:foo,bar,... ``` ## same: 要和符合某個欄位的驗證規則 ``` same:field ``` # (validation_rule)_if 當 anotherfield 是 value 時,欄位會需要符合 validation_rule ## accepted_if ``` accepted_if:anotherfield,value,... ``` 常用在服務條款(Terms of Service) ## declined_if ``` declined_if:anotherfield,value,... ``` ## exclude_if ``` exclude_if:anotherfield,value ``` ## missing_if ``` missing_if:anotherfield,value,... ``` ## present_if ``` present_if:anotherfield,value,... ``` ## prohibited_if ``` prohibited_if:anotherfield,value,... ``` ## required_if ``` required_if:anotherfield,value,... ``` # (validation_rule)_unless > 當 anotherfield 是 value 時,欄位不需要符合 validation_rule,但不等於 value 時需要符合 validation_rule ## exclude_unless ``` exclude_unless:anotherfield,value ``` ## missing_unless ``` missing_unless:anotherfield,value ``` ## required_unless ``` required_unless:anotherfield,value,... ``` ## present_unless ``` present_unless:anotherfield,value ``` ## prohibited_unless ``` prohibited_unless:anotherfield,value,... ``` # (validation_rule)_with 當特定欄位出現時,這個欄位要符合 validation_rule ## exclude_with ``` exclude_with:anotherfield ``` ## missing_with ``` missing_with:foo,bar,... ``` ## present_with ``` present_with:foo,bar,... ``` ## required_with: 當有任一其它欄位是 required 時,這個欄位要是 required ``` required_with:foo,bar,... ``` # (validation_rule)_without 當 anotherfield 沒有 present 時,這個欄位必需要符合 validation_rule ## exclude_without ``` exclude_without:anotherfield ``` ## required_without: 不是 required 時,這個欄位需要是 required ``` required_without:foo,bar,... ``` # (validation_rule)_with_all 當所有 anothrtfield 都是 present,這個欄位要符合 validation_rule ## missing_with_all ``` missing_with_all:foo,bar,... ``` ## present_with_all ``` present_with_all:foo,bar,... ``` ## required_with_all: 當所有 anothrtfield 都是 required,這個欄位要是 required ``` required_with_all:foo,bar,... ```