1.視圖中的表單
<div class="errors">
@if(count($errors)>0)
<div class="box-body">
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i>錯誤:</h4>
@foreach($errors->all() as $error)
{{$error}}
@endforeach
</div>
</div>
@endif
</div>
<!-- 下面是一個簡單的表單提交(由于是展示案例 沒有寫樣式) -->
<form action="/test" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<label> 推薦碼:</label> <input type="text" name="code">
<label>人員名稱:</label> <input type="text" name="name">
<label>所屬單位:</label> <input type="text" name="team">
<label>年 齡:</label> <input type="number" name="age">
<label>畢業(yè)院校:</label> <input type="text" name="school">
<label>產(chǎn)品案例網(wǎng)址:</label> <input type="url" name="url">
<button type="submit" class="btn btn-block btn-social">添加數(shù)據(jù)</button>
</form>
2.控制器中驗證數(shù)據(jù)
表單的發(fā)送post請求,到test路由中 發(fā)送了_token, code, name, team, age, school, url 7個數(shù)據(jù);
如果我的路由是以Restfulapi的寫法,它會根據(jù)我的提交方式自己找到控制器中對應的store方法:
Route::resource('test', 'TestController');
完整的控制器驗證方法如下(Validate驗證方式)
public function store(Request $request) { // $data = \Input::all(); // 數(shù)據(jù)驗證 $this->validate($request, [ 'code' => 'required|digits:32', //必填 必須32位 'name' => 'required|min:2|max:16', //必填 最小2位 最大16位 'team' => 'required|string', //必填 字符串 'age' => 'required|numeric', //必填 數(shù)值 'school' => 'required|max:255', //必填 最大255位 'url' => 'required|url' //必填 必須是網(wǎng)址 ]); // 以上是表單驗證 沒有驗證成功是不會走下面的邏輯程序的 而且頁面上會響應出拋出的異常信息 .... .... ....}
// 規(guī)則 $rules = array( 'code' => 'required|digits:32', //必填 必須32位 'name' => 'required|min:2|max:16', //必填 最小2位 最大16位 'team' => 'required|string', //必填 字符串 'age' => 'required|numeric', //必填 數(shù)值 'school' => 'required|max:255', //必填 最大255位 'url' => 'required|url' //必填 必須是網(wǎng)址 ); // 驗證器 $validator = \Validator::make($data,$rules); // 進行驗證 if($validator->passes()){ // 驗證成功后的業(yè)務邏輯 ... ... }else{ // 失敗后 返回提交頁的頁面并拋出錯誤 return back()->withErrors('在這里自定義錯誤原因!'); }
驗證規(guī)則大全
accepted
在驗證中該字段的值必須是yes、on、1或true,這在“同意服務協(xié)議”時很有用。
active_url
該字段必須是一個基于PHP函數(shù)checkdnsrr 的有效URL
after:date
該字段必須是給定日期后的一個值,日期將會通過PHP函數(shù)strtotime傳遞:
'start_date' => 'required|date|after:tomorrow'
你可以指定另外一個比較字段而不是使用strtotime驗證傳遞的日期字符串:
'finish_date' => 'required|date|after:start_date'
alpha
該字段必須是字母
alpha_dash
該字段可以包含字母和數(shù)字,以及破折號和下劃線
alpha_num
該字段必須是字母或數(shù)字
array
該字段必須是PHP數(shù)組
before:date
驗證字段必須是指定日期之前的一個數(shù)值,該日期將會傳遞給PHP strtotime函數(shù)。
between:min,max
驗證字段尺寸在給定的最小值和最大值之間,字符串、數(shù)值和文件都可以使用該規(guī)則
boolean
驗證字段必須可以被轉化為boolean,接收true, false, 1,0, "1", 和 "0"等輸入。
confirmed
驗證字段必須有一個匹配字段fooconfirmation,例如,如果驗證字段是password,必須輸入一個與之匹配的passwordconfirmation字段
date
驗證字段必須是一個基于PHP strtotime函數(shù)的有效日期
date_format:format
驗證字段必須匹配指定格式,該格式將使用PHP函數(shù)dateparsefromformat進行驗證。你應該在驗證字段時使用date或dateformat
different:field
驗證字段必須是一個和指定字段不同的值
digits:value
驗證字段必須是數(shù)字且長度為value指定的值
digits_between:min,max
驗證字段數(shù)值長度必須介于最小值和最大值之間
dimensions
驗證的圖片尺寸必須滿足該規(guī)定參數(shù)指定的約束條件:
'avatar' => 'dimensions:min_width=100,min_height=200'
有效的約束條件包括:min_width, max_width, min_height, max_height, width, height, ratio
ratio約束應該是寬度/高度,這可以通過表達式3/2或浮點數(shù)1.5來表示:
'avatar' => 'dimensions:ratio=3/2'
distinct
處理數(shù)組時,驗證字段不能包含重復值:
'foo.*.id' => 'distinct'
email
驗證字段必須是格式化的電子郵件地址 exists:table,column
驗證字段必須存在于指定數(shù)據(jù)表
基本使用:
'state' => 'exists:states'
指定自定義列名:
'state' => 'exists:states,abbreviation'
還可以添加更多查詢條件到where查詢子句:
'email' => 'exists:staff,email,account_id,1'
這些條件還可以包含!:
'email' => 'exists:staff,email,role,!admin'
還可以傳遞NULL或NOT NULL到where子句:
'email' => 'exists:staff,email,deleted_at,NULL'
'email' => 'exists:staff,email,deleted_at,NOT_NULL'
有時,你可能需要為exists查詢指定要使用的數(shù)據(jù)庫連接,這可以通過在表名前通過.前置數(shù)據(jù)庫連接來實現(xiàn):
'email' => 'exists:connection.staff,email'
file
該驗證字段必須是上傳成功的文件
filled
該驗證字段如果存在則不能為空
image
驗證文件必須是圖片(jpeg、png、bmp、gif或者svg) in:foo,bar…
驗證字段值必須在給定的列表中
in_array:另一個字段
驗證字段必須在另一個字段中存在
integer
驗證字段必須是整型
ip
驗證字段必須是IP地址
JSON
驗證字段必須是有效的JSON字符串
max:value
驗證字段必須小于等于最大值,和字符串、數(shù)值、文件字段的size規(guī)則一起使用 mimetypes:text/plain…
驗證文件必須匹配給定的MIME文件類型之一:
'video' => 'mimetypes:video/avi,video/mpeg,video/quicktime'
為了判斷上傳文件的MIME類型,框架將會讀取文件內(nèi)容來猜測MIME類型,這可能會和客戶端MIME類型不同。 mimes:foo,bar,…
驗證文件的MIMIE類型必須是該規(guī)則列出的擴展類型中的一個
MIMIE規(guī)則的基本使用:
'photo' => 'mimes:jpeg,bmp,png'
盡管你只需要指定擴展,該規(guī)則實際上驗證的是通過讀取文件內(nèi)容獲取到的文件MIME類型。 完整的MIME類型列表及其相應的擴展可以在這里找到:http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
min:value
驗證字段的最小值,和字符串、數(shù)值、文件字段的size規(guī)則一起使用
nullable
驗證字段必須為null,這在驗證一些可以為null的原生數(shù)據(jù)如整型或字符串時很有用。
not_in:foo,bar,…
驗證字段值不在給定列表中
numeric
驗證字段必須是數(shù)值
present
驗證字段必須出現(xiàn)在輸入數(shù)據(jù)中但不能為空。
regex:pattern
驗證字段必須匹配給定正則表達式
_注:使用regex模式時,規(guī)則必須放在數(shù)組中,而不能使用管道分隔符,尤其是正則表達式中使用管道符號時。_
required
輸入字段值不能為空,以下情況字段值都為空:
值為null
值是空字符串
值是空數(shù)組或者空的Coutable對象
值是上傳文件但路徑為空
required_if:anotherfield,value,…
驗證字段在另一個字段等于指定值value時是必須的
required_unless:anotherfield,value,…
除了 anotherfield 字段等于value,驗證字段不能空
required_with:foo,bar,…
驗證字段只有在任一其它指定字段存在的話才是必須的
required_with_all:foo,bar,…
驗證字段只有在所有指定字段存在的情況下才是必須的
required_without:foo,bar,…
驗證字段只有當任一指定字段不存在的情況下才是必須的
required_without_all:foo,bar,…
驗證字段只有當所有指定字段不存在的情況下才是必須的
same:field
給定字段和驗證字段必須匹配
size:value
驗證字段必須有和給定值value相匹配的尺寸,對字符串而言,value是相應的字符數(shù)目;對數(shù)值而言,value是給定整型值;對文件而言,value是相應的文件字節(jié)數(shù)
string
驗證字段必須是字符串
timezone
驗證字符必須是基于PHP函數(shù)timezoneidentifierslist的有效時區(qū)標識
unique:table,column,except,idColumn
驗證字段在給定數(shù)據(jù)表上必須是唯一的,如果不指定column選項,字段名將作為默認column。
指定自定義列名:
'email' => 'unique:users,email_address'
自定義數(shù)據(jù)庫連接
有時候,你可能需要自定義驗證器生成的數(shù)據(jù)庫連接,正如上面所看到的,設置unique:users作為驗證規(guī)則將會使用默認數(shù)據(jù)庫連接來查詢數(shù)據(jù)庫。要覆蓋默認連接,在數(shù)據(jù)表名后使用“.”指定連接:
'email' => 'unique:connection.users,email_address'
強制一個唯一規(guī)則來忽略給定ID:
有時候,你可能希望在唯一檢查時忽略給定ID,例如,考慮一個包含用戶名、郵箱地址和位置的”更新屬性“界面,當然,你將會驗證郵箱地址是唯一的,然而,如果用戶只改變用戶名字段而并沒有改變郵箱字段,你不想要因為用戶已經(jīng)擁有該郵箱地址而拋出驗證錯誤,你只想要在用戶提供的郵箱已經(jīng)被別人使用的情況下才拋出驗證錯誤,要告訴唯一規(guī)則忽略用戶ID,可以傳遞ID作為第三個參數(shù):
'email' => 'unique:users,email_address,'.$user->id
如果你的數(shù)據(jù)表使用主鍵字段不是id,可以指定第四個輸入?yún)?shù):
'email' => 'unique:users,email_address,'.$user->id.',user_id'
添加額外的where子句:
還可以指定更多條件給where子句:
'email' => 'unique:users,email_address,NULL,id,account_id,1'
在上述規(guī)則中,只有account_id為1記錄才會進行唯一性檢查。
url
驗證字段必須是基于PHP函數(shù)filter_var過濾的的有效URL
size:value
字段值的尺寸需符合給定 value 值。對于字串來說,value 為需符合的字串長度。對于數(shù)字來說,value 為需符合的整數(shù)值。對于文件來說,value 為需符合的文件大小(單位 kb)。