CI框架表單驗(yàn)證實(shí)例詳解
本文實(shí)例講述了CI框架表單驗(yàn)證的方法。分享給大家供大家參考,具體如下:
1、form頭部信息的自動(dòng)輸出函數(shù)(view)
<?php $attributes = array('class' => 'email', 'id' => 'myform'); echo form_open('email/send', $attributes); //上面一行代碼輸出: //<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" id="myform" class="email"/> /* * form_open_multipart() * 函數(shù)用法同上,加上了文件上傳的信息 上傳方式默認(rèn)為post */ ?>
2、設(shè)置驗(yàn)證規(guī)則(controller)
<?php //注意驗(yàn)證規(guī)則的變量名必須設(shè)置成 config $config = array( array( 'field' => 'username', 'label' => '用戶名', 'rules' => 'required' ), array( 'field' => 'password', 'label' => '密碼', 'rules' => 'required' ), array( 'field' => 'passconf', 'label' => '確認(rèn)密碼', 'rules' => 'required|matches[password]' ), array( 'field' => 'tel', 'label' => '手機(jī)', 'rules' => 'required|integer|exact_length[11]'), array( 'field' => 'email', 'label' => '郵箱', 'rules' => 'required|valid_email' ) ); //上面的會(huì)自動(dòng) //單獨(dú)設(shè)置規(guī)則 $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5'); $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required'); $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); ?>
3、規(guī)則對(duì)應(yīng)的錯(cuò)誤提示(controller)
<?php $this->form_validation->set_message('required', '必須填寫'); $this->form_validation->set_message('valid_email', '不是有效的email'); ?>
4、運(yùn)行檢查錯(cuò)誤信息(controller)
<?php $this->load->helper(array('form', 'url')); //加載CI表單驗(yàn)證庫(kù) $this->load->library('form_validation'); //---------------------------------------- # 驗(yàn)證規(guī)則及錯(cuò)誤信息代碼放在這里 //---------------------------------------- if ($this->form_validation->run() == FALSE){ //提交失敗 重新加載表單部分 $this->load->view('myform'); }else{ //提交成功 表單處理 //跳轉(zhuǎn)成功頁(yè)面 $this->load->view('formsuccess'); } }
5、錯(cuò)誤信息的輸出函數(shù)(view)
<?php //1.一股腦兒的全部輸出(放在表單標(biāo)簽的上方即可) echo validation_errors(); //2.針對(duì)單個(gè)表單單獨(dú)輸出(放在單個(gè)標(biāo)簽附近 參數(shù)為對(duì)應(yīng)表單元素的域名) echo form_error('password'); //3.針對(duì)單個(gè)表單輸出的時(shí)候 需要修改定界符 顯示錯(cuò)誤信息樣式(控制器里設(shè)置) $this->form_validation->set_error_delimiters('<span class="error">', '</span>'); //設(shè)置成內(nèi)聯(lián)元素比較好 ?>
6、錯(cuò)誤后 重新回填表單(view)
<?php //一般元素 回填(放在標(biāo)簽的values屬性中輸出) echo set_value('email'); //特殊元素select/checkbox/radio 第三個(gè)參數(shù)為true時(shí) 默認(rèn)被選中 //第二個(gè)參數(shù) 是對(duì)應(yīng)的表單元素的實(shí)際值 echo set_select('myselect', 'three'); //放在option的空白屬性里 echo set_checkbox('mycheck[]', '1'); //放在checkbox的空白屬性里 echo set_radio('myradio', '2'); //放在radio的空白屬性里 ?>
html代碼:
<html> <head> <title>My Form</title> </head> <body> <?php echo validation_errors(); ?> <?php echo form_open('form'); ?> <h5>Username</h5> <input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" /> <?php echo form_error('username'); ?> <h5>Password</h5> <input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" /> <?php echo form_error('password'); ?> <h5>Password Confirm</h5> <input type="text" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" /> <?php echo form_error('passconf'); ?> <h5>Email Address</h5> <input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" /> <?php echo form_error('email'); ?> <div><input type="submit" value="Submit" /></div> </form> </body> </html>
更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
淺談php serialize()與unserialize()的用法
本篇文章是對(duì)php中的serialize()與unserialize()的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06將PHP程序中返回的JSON格式數(shù)據(jù)用gzip壓縮輸出的方法
這篇文章主要介紹了將PHP中返回的JSON格式數(shù)據(jù)用gzip壓縮輸出的方法,文中示例環(huán)境為L(zhǎng)inux系統(tǒng)與Apache服務(wù)器,需要的朋友可以參考下2016-03-03php計(jì)算當(dāng)前程序執(zhí)行時(shí)間示例
這篇文章主要介紹了php計(jì)算當(dāng)前程序執(zhí)行時(shí)間示例,需要的朋友可以參考下2014-04-04根據(jù)ip調(diào)用新浪api獲取城市名并轉(zhuǎn)成拼音
這篇文章主要介紹了根據(jù)ip調(diào)用新浪api獲取城市名并轉(zhuǎn)成拼音的示例,,需要的朋友可以參考下2014-03-03利用php+mysql來(lái)做一個(gè)功能強(qiáng)大的在線計(jì)算器
有天在努力的搜索計(jì)算器,發(fā)現(xiàn)都是JavaScript,而且要一個(gè)個(gè)地點(diǎn)擊,并且不能記錄,輸入計(jì)算式子時(shí)容易出錯(cuò),于是就想了想該怎樣才能讓它好用點(diǎn)呢,能夠用鍵盤直接輸入。2010-10-10Laravel5.1 框架數(shù)據(jù)庫(kù)查詢構(gòu)建器用法實(shí)例詳解
這篇文章主要介紹了Laravel5.1 框架數(shù)據(jù)庫(kù)查詢構(gòu)建器用法,結(jié)合實(shí)例形式詳細(xì)分析了laravel5.1框架查詢構(gòu)造器相關(guān)原理、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01yii2.0數(shù)據(jù)庫(kù)遷移教程【多個(gè)數(shù)據(jù)庫(kù)同時(shí)同步數(shù)據(jù)】
這篇文章主要介紹了yii2.0數(shù)據(jù)庫(kù)遷移的方法,可實(shí)現(xiàn)多個(gè)數(shù)據(jù)庫(kù)同時(shí)同步數(shù)據(jù)的功能,較為詳細(xì)的分析了Yii2針對(duì)遷移的創(chuàng)建、提交、重做及自定義遷移的相關(guān)概念與使用方法,需要的朋友可以參考下2016-10-10ThinkPHP實(shí)現(xiàn)跨模塊調(diào)用操作方法概述
使用 $this 可以調(diào)用當(dāng)前模塊內(nèi)的方法,但是很多情況下經(jīng)常會(huì)在當(dāng)前模塊中調(diào)用其他模塊的方法。這篇文章主要介紹了ThinkPHP跨模塊調(diào)用操作,需要的朋友可以參考下2014-06-06