?? ?? ??
$validator = Validator::make( array('name' => 'Dayle'), array('name' => 'required|min:5') );
make ??? ???? ? ?? ????? ??? ?????, ? ?? ????? ???? ???? ?? ?? ?????.
?? ??? ?? ??? "|" ??? ????? ??? ?? ??? ??? ? ????.
??? ?? ??? ?? ?? ??
$validator = Validator::make( array('name' => 'Dayle'), array('name' => array('required', 'min:5')) );
Validator ????? ???? ??(?? ??) ??? ???? ? ??? ??? ??? ? ????.
if ($validator->fails()) { // The given data did not pass validation }
??? ???? ????? ?? ???? ?? ? ????.
$messages = $validator->messages();
failed ??? ???? ?? ??? ?? ??? ??? ??? ?? ??? ??? ?? ????.
$failed = $validator->failed();
?? ??? ??
Validator ???? ??, MIME ?? ?? ?? ???? ???? ?? ? ?? ??? ?? ??? ?????. ??? ???? ??? ? ?? ??? ??? ????? ??? ???? ??? ??? ? ????.
?? ??? ??
Validator ?????? ??? ??? ???? ?? ??? ??? ?? ??? ??? ?? ??? MessageBag ????? ?????.
???? ?? ? ?? ?? ??? ????
echo $messages->first('email');
???? ?? ?? ?? ??? ????
foreach ($messages->get('email') as $message) { // }
?? ???? ?? ?? ?? ??? ????
foreach ($messages->all() as $message) { // }
???? ?? ???? ??? ??
if ($messages->has('email')) { // }
?? ??? ?? ??? ??
echo $messages->first('email', '<p>:message</p>');
??: ????? ???? ???? ???? ?????. ???? ?? ????? ??? ?????.
?? ???? ?? ?? ??? ????
foreach ($messages->all('<li>:message</li>') as $message) { // }
?? ??? ? ??
??? ??? ??? ??? ??? ?? ??? ? ?? ?? ??? ?????. ??. ?? Lavavel?? ?? ??? ? ????. ?? ??? ?? ?? ?????.
Route::get('register', function() { return View::make('user.register'); }); Route::post('register', function() { $rules = array(...); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { return Redirect::to('register')->withErrors($validator); } });
??? ???? withErrors ??? ???? Validator ????? Redirect? ?????. ? ??? ??? ??? ?? ???? ?? ?? ?? ?? ? ??? ? ??? ???.
??? GET ??? ??? ?? ???? ????? ???? ??? ????. ?? Laravel? ?? ??? ??? ??? ???? ??? ??? ???? ?? ????? ?????. ??? ?? ??? ???? $errors ??? ?? ?? ???? ??? ? ???? $errors? ?? ???? ????? ????? ???? ??? ? ????. $errors ??? MessageBag ???? ????? ???.
??? ?? ? ??? ???? ???? $errors ??? ??? ? ????.
<?php echo $errors->first('email'); ?>
?? ??? ??? ?? ??
??? ?? ??? ?? ?????. ??? ?? ?? ? ?? ??:
Accepted Active URL After (Date) Alpha Alpha Dash Alpha Numeric Before (Date) Between Confirmed Date Date Format Different 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 Without Same Size Unique (Database)
accepted
??? ?? ? ??? ?? yes, on ?? 1??? ???. ?? ??? ??? ?? ?? ??? ??? ? ?????.
active_url
PHP ?? checkdnsrr? ?? ? ??? ?? ??? URL??? ??? ?????.
after:date
? ??? ?? PHP ?? strtotime? ?? ???? ??? ?? ???? ??? ?????.
alpha
? ??? ?? ?? ??? ??? ????? ??? ?????.
alpha_dash
? ??? ?? ??, ??, ?? ?? ?? ???? ????? ??? ?????.
alpha_num
? ??? ???? ?? ??? ???? ????? ???.
before:date
? ??? ?? ??? ?? ????? ??? ???? ??? PHP ?? strtotime? ?? ?????.
between:min,max
? ??? ?? ??? ???? ??? ??? ??? ??? ?????. ???, ??, ??? ?? ??? ???? ?????.
confirmed
? ??? ?? ??? ?? foo_confirmation? ?? ???? ???. ?? ??, ? ??? ?? ???? ???? ?? ??? ????? ?? ??? ??? ????_?? ??? ??? ???.
date
PHP ?? strtotime? ?? ? ??? ?? ??? ???? ?????.
date_format:format
? ??? ?? PHP ?? date_parse_from_format? ?? ??? ??? ??? ???? ??? ?????.
??:??
? ??? ?? ??? ??? ?? ??? ??? ?????.
???
? ??? ?? ??? ??? ???? ?????.
exists:table,column
? ??? ?? ??? ??????? ???? ???? ??? ?????.
Exists ??? ???
'state' => 'exists:states'
? ?? ??
'state' => 'exists:states,abbreviation'
? ???? ????. ?? ? ?? ??? ??? ?? ????. "??" ?? ?????.
'email' => 'exists:staff,email,account_id,1'
image
? ??? ???? ?? ???(jpeg, png, bmp ?? gif)?? ???.
in:foo,bar,...
? ??? ?? ??? ??? ??? ??? ?????.
??
? ??? ?? ???? ??? ?????.
? ??? ?? ??? IP ???? ??? ?????.
max:value
? ??? ?? ????? ??? ??? ?????. ???, ??, ??? ?? ??? ???? ?????.
Mimes:foo,bar,...
? ??? ???? ??? ??? MIME ??? ??? ??? ??? ???.
MIME 規(guī)則的基礎(chǔ)使用
'photo' => 'mimes:jpeg,bmp,png'
min:value
驗(yàn)證此規(guī)則的值必須大于最小值 value。字符串、數(shù)字以及文件都將使用大小規(guī)則進(jìn)行比較。
not_in:foo,bar,...
驗(yàn)證此規(guī)則的值必須在給定的列表中不存在。
numeric
驗(yàn)證此規(guī)則的值必須是一個(gè)數(shù)字。
regex:pattern
驗(yàn)證此規(guī)則的值必須符合給定的正則表達(dá)式。
注意: 當(dāng)使用 regex 模式的時(shí)候,有必要使用數(shù)組指定規(guī)則,而不是管道分隔符,特別是正則表達(dá)式中包含一個(gè)管道字符的時(shí)候。
required
驗(yàn)證此規(guī)則的值必須在輸入數(shù)據(jù)中存在。
required_if:field,value
當(dāng)指定的域?yàn)槟硞€(gè)值的時(shí)候,驗(yàn)證此規(guī)則的值必須存在。
required_with:foo,bar,...
僅當(dāng)指定的域存在的時(shí)候,驗(yàn)證此規(guī)則的值必須存在。
required_without:foo,bar,...
僅當(dāng)指定的域不存在的時(shí)候,驗(yàn)證此規(guī)則的值必須存在。
same:field
驗(yàn)證此規(guī)則的值必須與給定域的值相同。
size:value
驗(yàn)證此規(guī)則的值的大小必須與給定的 value 相同。對(duì)于字符串,value 代表字符的個(gè)數(shù);對(duì)于數(shù)字,value 代表它的整數(shù)值,對(duì)于文件,value 代表文件以KB為單位的大小。
unique:table,column,except,idColumn
驗(yàn)證此規(guī)則的值必須在給定的數(shù)據(jù)庫(kù)的表中唯一。如果 column 沒(méi)有被指定,將使用該域的名字。
Unique 規(guī)則的基礎(chǔ)使用
'email' => 'unique:users' 指定列名 'email' => 'unique:users,email_address' 強(qiáng)制忽略一個(gè)給定的 ID 'email' => 'unique:users,email_address,10'
url
驗(yàn)證此規(guī)則的值必須是一個(gè)合法的 URL。
定制錯(cuò)誤消息
如果有需要,您可以使用定制的錯(cuò)誤消息代替默認(rèn)的消息。這里有好幾種定制錯(cuò)誤消息的方法。
傳遞定制消息到驗(yàn)證器
$messages = array( 'required' => 'The :attribute field is required.', ); $validator = Validator::make($input, $rules, $messages);
注意: :attribute 占位符將被實(shí)際的進(jìn)行驗(yàn)證的域的名字代替,您也可以在錯(cuò)誤消息中使用其他占位符。
其他驗(yàn)證占位符
$messages = array( 'same' => 'The :attribute and :other must match.', 'size' => 'The :attribute must be exactly :size.', 'between' => 'The :attribute must be between :min - :max.', 'in' => 'The :attribute must be one of the following types: :values', );
有些時(shí)候,您可能希望只對(duì)一個(gè)指定的域指定定制的錯(cuò)誤消息:
對(duì)一個(gè)指定的域指定定制的錯(cuò)誤消息
$messages = array( 'email.required' => 'We need to know your e-mail address!', );
在一些情況下,您可能希望在一個(gè)語(yǔ)言文件中指定錯(cuò)誤消息而不是直接傳遞給 Validator。為了實(shí)現(xiàn)這個(gè)目的,請(qǐng)?jiān)?app/lang/xx/validation.php 文件中添加您的定制消息到 custom 數(shù)組。
在語(yǔ)言文件中指定錯(cuò)誤消息
'custom' => array( 'email' => array( 'required' => 'We need to know your e-mail address!', ), ),
定制驗(yàn)證規(guī)則
Laravel 提供了一系列的有用的驗(yàn)證規(guī)則;但是,您可能希望添加自己的驗(yàn)證規(guī)則。其中一種方法是使用 Validator::extend 函數(shù)注冊(cè)定制的驗(yàn)證規(guī)則:
注冊(cè)一個(gè)定制的驗(yàn)證規(guī)則
Validator::extend('foo', function($attribute, $value, $parameters) { return $value == 'foo'; });
注意: 傳遞給 extend 函數(shù)的規(guī)則的名字必須符合 "snake cased" 命名規(guī)則。
定制的驗(yàn)證器接受三個(gè)參數(shù):待驗(yàn)證屬性的名字、待驗(yàn)證屬性的值以及傳遞給這個(gè)規(guī)則的參數(shù)。
您也可以傳遞一個(gè)類的函數(shù)到 extend 函數(shù),而不是使用閉包:
Validator::extend('foo', 'FooValidator@validate');
注意您需要為您的定制規(guī)則定義錯(cuò)誤消息。您既可以使用一個(gè)行內(nèi)的定制消息數(shù)組,也可以在驗(yàn)證語(yǔ)言文件中進(jìn)行添加。
您也可以擴(kuò)展 Validator 類本身,而不是使用閉包回調(diào)擴(kuò)展驗(yàn)證器。為了實(shí)現(xiàn)這個(gè)目的,添加一個(gè)繼承自 Illuminate\Validation\Validator 的驗(yàn)證器類。您可以添加在類中添加以 validate 開(kāi)頭的驗(yàn)證函數(shù):
擴(kuò)展驗(yàn)證器類
<?php class CustomValidator extends Illuminate\Validation\Validator { public function validateFoo($attribute, $value, $parameters) { return $value == 'foo'; } }
下面,您需要注冊(cè)定制的驗(yàn)證器擴(kuò)展:
您需要注冊(cè)定制的驗(yàn)證器擴(kuò)展
Validator::resolver(function($translator, $data, $rules, $messages) { return new CustomValidator($translator, $data, $rules, $messages); });
當(dāng)創(chuàng)建一個(gè)定制的驗(yàn)證規(guī)則,您有時(shí)需要為錯(cuò)誤消息定義一個(gè)定制的占位符。為了實(shí)現(xiàn)它,您可以像上面那樣創(chuàng)建一個(gè)定制的驗(yàn)證器,并且在驗(yàn)證器中添加一個(gè) replaceXXX 函數(shù):
protected function replaceFoo($message, $attribute, $rule, $parameters) { return str_replace(':foo', $parameters[0], $message); }
更多Laravel框架表單驗(yàn)證詳解相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)