|
在開始ZF擴(kuò)展之前,推薦先看看ZF手冊(cè)中的一些命令規(guī)范(ZF推薦使用),同時(shí)希望讀者對(duì)ZF有較好的理解。如果沒有,可以先上phpCHIAN的ZF版本詳細(xì)了解,或者到phpeye查找相關(guān)資料。
ZF的validator提供了強(qiáng)大的驗(yàn)證功能,但在實(shí)際的操作中還是過于煩瑣。比如說驗(yàn)證郵件,是用ZF的代碼如下
<?php
require_once 'Zend/Validate/EmailAddress.php';
$validator = new Zend_Validate_EmailAddress();
if ($validator->isValid($email)) {
// email appears to be valid
} else {
// email is invalid; print the reasons
foreach ($validator->getMessages() as $message) {
echo "$message/n";
}
}
?>
有沒有發(fā)現(xiàn),還是很類似我們不使用ZF的驗(yàn)證方式。只不過ZF幫我們把郵件驗(yàn)證的細(xì)節(jié)封裝好了。那么我們?nèi)绾魏喕蛇@樣效果呢?(下面是我擴(kuò)展后的調(diào)用方式)
<?php
$validate = new phpbean_Validate();
$validate -> set_breakOnFailure(false);
$validate -> add('email',new Zend_Validate_EmailAddress(),'郵件地址不正確!');
$validate -> add('username',new Zend_Validate_StringLength(3,15),'用戶名長度必須在3到15之間!/'%value%/'不滿足條件');
$validate -> add('password',new Zend_Validate_StringLength(6,20),'密碼長度必須在6到20之間!');
$validate -> add('password',new phpbean_Validate_isEqual($_POST['repassword']),'兩次輸入密碼不匹配');
$authcode = new phpbean_Img_Code();
$validate -> add('yanxue8_authcode',new phpbean_Validate_isEqual($authcode->authcode($_POST['yanxue8_authcode_mdcode'],'DECODE')),'驗(yàn)證碼不匹配!');
if( !$validate -> validator($_POST) ){
error_page('注冊(cè)失敗',$validate->getMessageText());
}
?>
用上面這種方式一方面代碼清晰,另一方面也有利同意的出錯(cuò)處理。那么如何做到這樣呢?
關(guān)鍵是phpbean_Validate這個(gè)類。
其實(shí)實(shí)現(xiàn)起來很簡單,phpbean_Validate::add()方法是把一條條的驗(yàn)證規(guī)則加入進(jìn)來。然后調(diào)用phpbean_Validate::validator()來驗(yàn)證就OK了。
具體實(shí)現(xiàn)步驟如下:
首先,在zend的同級(jí)目錄中增加一個(gè)phpbean文件夾,然后在里面增加一個(gè)Validator.php文件。
然后,在validator.php文件加入phpbean_Validate這個(gè)類的定義。注意(你可以修改成自己的文件名和路徑名,但注意一定要和類的名稱保持一致)。
這里,我給出我的phpbean_Validate類的實(shí)現(xiàn)過程,僅供參考。
<?
class phpbean_Validate{
protected $_fileds =array();
protected $_message = array();
protected $_breakOnFailure = true;
public function set_breakOnFailure($value){
$this->_breakOnFailure = $value;
}
public function add($key,$validate,$message='',$breakOnFailure=''){
if( empty($breakOnFailure) ) $breakOnFailure = $this->_breakOnFailure;
$this->_fileds[] = array($key,$validate,$message,$breakOnFailure);
return $this;
}
public function validator($array = array()){
if(empty($array)) $array = $_POST;
if (is_array($this->_fileds)) {
foreach ($this->_fileds as $filed){
list($key,$validate,$message,$breakOnFailure) = $filed;
if(empty($key)){
if(!$validate){
$this->_message[][] = $message;
if($breakOnFailure) break;
}
continue;
}
if(!empty($message)) $validate->setMessage($message);
if( !$validate->isValid($array[$key]) ){
$this->_message[$key][] = $validate->getMessages();
if($breakOnFailure) break;
}
}
if(!empty($this->_message))return false;
return true;
}
return true;
}
public function getMessage(){
return $this->_message;
}
public function getMessageText(){
$str = '';
foreach ($this->_message as $ms){
foreach ($ms as $m) $str .= $m[0]."/n";
}
return $str;
}
}
?>
另外你還可以直接擴(kuò)展一些驗(yàn)證規(guī)則類。下篇我再詳細(xì)說。
php技術(shù):php擴(kuò)展ZF――Validate擴(kuò)展,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。