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ī)則對應(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)證庫
$this->load->library('form_validation');
//----------------------------------------
# 驗(yàn)證規(guī)則及錯(cuò)誤信息代碼放在這里
//----------------------------------------
if ($this->form_validation->run() == FALSE){
//提交失敗 重新加載表單部分
$this->load->view('myform');
}else{
//提交成功 表單處理
//跳轉(zhuǎn)成功頁面
$this->load->view('formsuccess');
}
}
5、錯(cuò)誤信息的輸出函數(shù)(view)
<?php
//1.一股腦兒的全部輸出(放在表單標(biāo)簽的上方即可)
echo validation_errors();
//2.針對單個(gè)表單單獨(dú)輸出(放在單個(gè)標(biāo)簽附近 參數(shù)為對應(yīng)表單元素的域名)
echo form_error('password');
//3.針對單個(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ù) 是對應(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ù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
Thinkphp自定義美化success和error提示跳轉(zhuǎn)頁面代碼實(shí)例
這篇文章主要介紹了Thinkphp自定義美化success和error提示跳轉(zhuǎn)頁面代碼實(shí)例,有需要的同學(xué)可以直接借鑒文中代碼,可以增加頁面的美觀和友好程度2021-03-03
Laravel框架實(shí)現(xiàn)利用監(jiān)聽器進(jìn)行sql語句記錄功能
這篇文章主要介紹了Laravel框架實(shí)現(xiàn)利用監(jiān)聽器進(jìn)行sql語句記錄功能,結(jié)合實(shí)例形式分析了Laravel框架監(jiān)聽器的創(chuàng)建、引入以及使用監(jiān)聽器記錄sql語句的相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
詳解thinkphp+redis+隊(duì)列的實(shí)現(xiàn)代碼
本篇文章主要介紹了thinkphp+redis+隊(duì)列的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
php實(shí)現(xiàn)的IMEI限制的短信驗(yàn)證碼發(fā)送類
本文給大家分享的是可以檢驗(yàn)手機(jī)號碼與IMEI的短信驗(yàn)證碼發(fā)送的php類,十分的實(shí)用,這里推薦給大家,有需要的小伙伴可以參考下。2015-05-05
php生成隨機(jī)密碼自定義函數(shù)代碼(簡單快速)
創(chuàng)建大量用戶時(shí)一個(gè)一個(gè)想密碼是讓人頭疼的事,使用php隨機(jī)生成一個(gè)安全可靠的密碼,又方便又快捷,可以添加自己想的字符串,可以用在FTP密碼、Mysql密碼、網(wǎng)站后臺(tái)密碼等地方2014-05-05
thinkphp3.x自定義Action、Model及View的簡單實(shí)現(xiàn)方法
這篇文章主要介紹了thinkphp3.x自定義Action、Model及View的簡單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了thinkPHP3.x自定義模型、視圖及控制器的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-05-05
thinkphp5加layui實(shí)現(xiàn)圖片上傳功能(帶圖片預(yù)覽)
這篇文章主要介紹了thinkphp5加layui實(shí)現(xiàn)圖片上傳功能(帶圖片預(yù)覽)的相關(guān)資料,需要的朋友可以參考下2023-03-03

