PHP Yii框架之表單驗(yàn)證規(guī)則大全
Yii是一個(gè)基于組件的高性能PHP框架,用于開(kāi)發(fā)大型Web應(yīng)用。Yii采用嚴(yán)格的OOP編寫(xiě),并有著完善的庫(kù)引用以及全面的教程。
廢話不多說(shuō)了,直接給大家貼代碼了。
<?php class ContactForm extends CFormModel { public $_id; public $contact;//聯(lián)系人 public $tel;//電話 public $fax;//傳真 public $zipcode;//郵編 public $addr;//地址 public $mobile;//手機(jī) public $email;//郵箱 public $website;//網(wǎng)址 public $qq;//QQ public $msn;//MSN public function rules() { return array( array('contact','required','on'=>'edit','message'=>'聯(lián)系人必須填寫(xiě).'), array('contact','length','on'=>'edit','min'=>2,'max'=>10,'tooShort'=>'聯(lián)系人長(zhǎng)度請(qǐng)控制在2-10個(gè)字符.','tooLong'=>'聯(lián)系人長(zhǎng)度請(qǐng)控制在2-10個(gè)字符.'), array('tel', 'match','pattern' => '/^(\d{3}-|\d{4}-)(\d{8}|\d{7})?$/','message' => '請(qǐng)輸入正確的電話號(hào)碼.'), array('fax', 'match','pattern' => '/^(\d{3}-|\d{4}-)(\d{8}|\d{7})?$/','message' => '請(qǐng)輸入正確的傳真號(hào)碼.'), array('mobile', 'match','pattern' => '/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/','message' => '請(qǐng)輸入正確的手機(jī)號(hào)碼.'), array('email','email','on'=>'edit','message'=>'郵箱輸入有誤.'), array('zipcode','required','on'=>'edit','message'=>'郵編必須填寫(xiě).'), array('zipcode','numerical','on'=>'edit','message'=>'郵編是6位數(shù)字.'), array('zipcode','length','on'=>'edit','min'=>6,'max'=>6,'tooShort'=>'郵編長(zhǎng)度為6位數(shù).','tooLong'=>'郵編長(zhǎng)度為6位數(shù).'), array('website','url','on'=>'edit','message'=>'網(wǎng)址輸入有誤.'), array('qq', 'match','pattern' => '/^[1-9]{1}[0-9]{4,11}$/','message' => '請(qǐng)輸入正確的QQ號(hào)碼.'), array('msn','email','on'=>'edit','message'=>'MSN輸入有誤.'), ); } }
完整示例:
public $password2;//非數(shù)據(jù)庫(kù)的字段,但是在view中需要用到 public $verify; //手機(jī)驗(yàn)證碼 public $fjg; //忘記號(hào)碼 /** * 映射數(shù)據(jù)庫(kù)表名 * @return string the associated database table name<br><br> * www.shouce.ren */ public function tableName() { return 'adm_user'; } /** * 驗(yàn)證規(guī)則 * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( //array('mobile_phone,name,status', 'required'), array('mobile_phone', 'unique'),//'message' => '該手機(jī)號(hào)已經(jīng)存在!' array('mobile_phone', 'match','pattern' => '/^(13|15|18)[0-9]{9}$/','message' => '請(qǐng)輸入正確的經(jīng)辦人手機(jī)號(hào)碼.'), //array('certificate_id', 'match','pattern' => '/(.jpg|.gif|.png|\d)$/','message' => '請(qǐng)重新選擇證書(shū)圖像并且后綴只能是jpg、gif、png格式.'), array('phone', 'match','pattern' => '/^(\d{3}-|\d{4}-)?(\d{8}|\d{7})?$/','message' => '請(qǐng)輸入正確的座機(jī)號(hào)碼.'), array('fax', 'match','pattern' => '/^(\d{3}-|\d{4}-)(\d{8}|\d{7})?$/','message' => '請(qǐng)輸入正確的傳真號(hào)碼.'), //array('email_address', 'match','pattern' => '/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/','message' => '請(qǐng)輸入正確的郵箱.'), array('email_address','email','message'=>'請(qǐng)輸入正確的郵箱.'), //驗(yàn)證密碼和確認(rèn)密碼 array("password2","compare","compareAttribute"=>"password","message"=>"兩次密碼不一致",'on'=>'register'), array("password2","compare","compareAttribute"=>"password","message"=>"兩次密碼不一致",'on'=>'regonter'), array('qq', 'match','pattern' => '/^[1-9]{1}[0-9]{4,11}$/','message' => '請(qǐng)輸入正確的QQ號(hào)碼.'), array('type,certificate_id,company_type, nationality,yyzz_id, status,level,create_by_id, create_time,update_time', 'numerical', 'integerOnly'=>true), array('verify', 'numerical', 'message' => '驗(yàn)證碼不正確','integerOnly'=>true), array('name,user_type,tuijianren', 'length', 'max'=>20), array('password', 'length', 'max'=>100), array('email_address,business', 'length', 'max'=>50), array('communication_address,money, yhzh,yhmc,industry, company, register_address,yhdh,shangbiao,zhuanli,gongshang', 'length', 'max'=>255), array('role_id','default', 'setOnEmpty'=>true, 'value'=>10), array('shangbiao','default', 'setOnEmpty'=>true, 'value'=>'0,0'), array('zhuanli','default', 'setOnEmpty'=>true, 'value'=>'0,0'), array('gongshang','default', 'setOnEmpty'=>true, 'value'=>'0,0'), array('password','default', 'setOnEmpty'=>true, 'value'=>'123456'), /*驗(yàn)證碼*/ array('verify','checkVerify', 'on'=>'register'), array('email_address','checkemail', 'on'=>'regonter'), // array('certificate_id', 'file','allowEmpty'=>true, // 'types'=>'jpg, gif, png, doc, txt', // 'maxSize'=>1024 * 1024 * 10, // 10MB // 'tooLarge'=>'文件大小不能超過(guò)10M!', // 'message'=>'請(qǐng)先上傳證書(shū)圖像.' // ), // The following rule is used by search(). // @todo Please remove those attributes that should not be searched. array('id,role_id,name, password,user_type, email_address,tuijianren,shangbiao,company_type,zhuanli,gongshang,money,yhzh,yhmc,yyzz_id,yhdh,type,level, phone, qq, mobile_phone, fax, communication_address, nationality, industry, company, business, register_address, certificate, status, create_by_id, create_time, update_time', 'safe', 'on'=>'search'), ); } /* * 手機(jī)驗(yàn)證碼校驗(yàn) */ public function checkVerify($attribute,$params) { $model=new Mess(); $d_title = $model->find(array('condition'=>'suij=:suij and tel=:tel and type>:type and time>:time','params'=>array(':suij'=>$this->verify,':tel'=>$this->mobile_phone,':type'=>0,':time'=>(time()-3600)),'select'=>array('id'))); //$d_title = $model->findByAttributes(array('suij'=>$this->verify,'tel'=>$this->mobile_phone),array('select'=>array('id'))); if($d_title['id']<1) { $this->addError('verify', "驗(yàn)證碼不正確。"); } else { if($this->password == $this->password2) { $model->updateAll(array('type'=>0),array('condition'=>'suij=:sj','params'=>array(':sj'=>$this->verify))); } } }
以上代碼就是關(guān)于PHP Yii框架之表單驗(yàn)證規(guī)則大全的全部?jī)?nèi)容,希望大家喜歡。
相關(guān)文章
ThinkPHP3.1新特性之查詢條件預(yù)處理簡(jiǎn)介
相比于ThinkPHP3.0版本對(duì)數(shù)組方式的查詢條件會(huì)進(jìn)行安全過(guò)濾而不支持字符串條件的安全過(guò)濾的情況,ThinkPHP3.1版本則增加了對(duì)條件字符串進(jìn)行預(yù)處理的支持,讓ORM的安全性更加得以保證。這篇文章主要介紹了ThinkPHP3.1的查詢條件預(yù)處理,需要的朋友可以參考下2014-06-06Laravel 框架基于自帶的用戶系統(tǒng)實(shí)現(xiàn)登錄注冊(cè)及錯(cuò)誤處理功能分析
這篇文章主要介紹了Laravel 框架基于自帶的用戶系統(tǒng)實(shí)現(xiàn)登錄注冊(cè)及錯(cuò)誤處理功能,結(jié)合實(shí)例形式分析了laravel框架自帶用戶系統(tǒng)的基本使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04thinkPHP5.1框架路由::get、post請(qǐng)求簡(jiǎn)單用法示例
這篇文章主要介紹了thinkPHP5.1框架路由::get、post請(qǐng)求簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了thinkPHP5.1路由get、post請(qǐng)求基本使用方法,需要的朋友可以參考下2019-05-05tp5(thinkPHP5)框架數(shù)據(jù)庫(kù)Db增刪改查常見(jiàn)操作總結(jié)
這篇文章主要介紹了tp5(thinkPHP5)框架數(shù)據(jù)庫(kù)Db增刪改查常見(jiàn)操作,結(jié)合實(shí)例形式總結(jié)分析了thinkPHP5框架數(shù)據(jù)庫(kù)的增刪改查常見(jiàn)操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-01-01解決phpmyadmin中缺少mysqli擴(kuò)展問(wèn)題的方法
本篇文章是對(duì)解決phpmyadmin中缺少mysqli擴(kuò)展問(wèn)題的方法進(jìn)行了詳細(xì)的介紹。需要的朋友參考下2013-05-05PHP中字符與字節(jié)的區(qū)別及字符串與字節(jié)轉(zhuǎn)換示例
在php中字符是可使用多種不同字符方案或代碼頁(yè)來(lái)表示的抽象實(shí)體。字節(jié)是通過(guò)網(wǎng)絡(luò)傳輸信息(或在硬盤(pán)或內(nèi)存中存儲(chǔ)信息)的單位。本文還通過(guò)實(shí)例給大家介紹了php中字符串與字節(jié)轉(zhuǎn)換示例,感興趣的朋友一起看看吧2016-10-10