1、CodeIgniter容許你爲單個表單域建立多個驗證規則,按順序層疊在一塊兒,你甚至能夠同時預先處理表單域數據。要設置驗證規則請使用set_rules() 函數:php
上面的函數使用 三個 參數做爲輸入:html
$this->form_validation->set_rules('username', 'Username', 'required'); $this->form_validation->set_rules('password', 'Password', 'required'); $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); $this->form_validation->set_rules('email', 'Email', 'required');
上面是一段示例代碼。數組
<?php class Form extends CI_Controller { function index() { $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->form_validation->set_rules('username', 'Username', 'required'); $this->form_validation->set_rules('password', 'Password', 'required'); $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); $this->form_validation->set_rules('email', 'Email', 'required'); if ($this->form_validation->run() == FALSE) { $this->load->view('myform'); } else { $this->load->view('formsuccess'); } } } ?>
控制器的內容如上。xss
2、使用數組設置驗證規則:函數
$config = array( array( 'field' => 'username', 'label' => 'Username', 'rules' => 'required' ), array( 'field' => 'password', 'label' => 'Password', 'rules' => 'required' ), array( 'field' => 'passconf', 'label' => 'Password Confirmation', 'rules' => 'required' ), array( 'field' => 'email', 'label' => 'Email', 'rules' => 'required' ) ); $this->form_validation->set_rules($config);
3、級聯規則:post
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]'); $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]'); $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
上面的代碼設置了一組規則:ui
4、預處理數據this
$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');
在上面的例子裏,咱們「修整(trimming,去掉字符串兩端空白)」了數據,轉換密碼爲MD5,將用戶名經過「xss_clean」函數處理去掉了有害數據。url
任何PHP自身接收一個參數的函數均可以被用做一個規則,好比 htmlspecialchars, trim, MD5, 等。spa
5、從新填充表單
set_value('field name')
不要忘記在set_value()函數中包含每一個表單域的名字!
<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" /> <h5>Password</h5> <input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" /> <h5>Password Confirm</h5> <input type="text" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" /> <h5>Email Address</h5> <input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" /> <div><input type="submit" value="Submit" /></div> </form> </body> </html>
6、設置錯誤信息
$this->form_validation->set_message('rule', 'Error Message');
你也能夠重寫語言文件中的錯誤信息. 例如, 你能夠這麼作來改變"required" 規則的信息:
$this->form_validation->set_message('required', 'Your custom message here');
7、更改錯誤定界符
在默認狀況下,表單驗證類會使用 (<p>) 標籤來分割每條錯誤信息,以達到分段效果。你能夠自行對其進行定義。
若是須要全局更改錯誤定界符, 能夠在你的控制器中,在表單驗證類加載以後添加以下代碼:
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
在此例中,咱們將定界符由系統默認的 <p> 更改成一個div標籤。
表單驗證類所提供的兩種顯示錯誤驗證信息的函數,分別能夠經過以下方法來設置它們的定界符:
<?php echo form_error('field name', '<div class="error">', '</div>'); ?>
或者:
<?php echo validation_errors('<div class="error">', '</div>'); ?>