控制器代码如下:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Form extends CI_Controller{
public function index(){
$this->load->helper(array('form','url'));
$this->load->library('form_validation');//验证初始化函数
$this->form_validation->set_rules('username', 'Username', arrary($this->users_model, 'valid_username'));
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
//级联规则
if ($this->form_validation->run()==FALSE)
{
$this->load->view('test/myform');
}
else{
$this->load->view('test/formsuccess');
}
}
public function username_check($str)
{
if($str=='test'){
$this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
return FALSE;
}
else{
return TRUE;
}
}
}
视图代码如下:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Form extends CI_Controller{
public function index(){
$this->load->helper(array('form','url'));
$this->load->library('form_validation');//验证初始化函数
$this->form_validation->set_rules('username', 'Username', arrary($this->users_model, 'valid_username'));
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
//级联规则
if ($this->form_validation->run()==FALSE)
{
$this->load->view('test/myform');
}
else{
$this->load->view('test/formsuccess');
}
}
public function username_check($str)
{
if($str=='test'){
$this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
return FALSE;
}
else{
return TRUE;
}
}
}
另一个页面:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Form extends CI_Controller{
public function index(){
$this->load->helper(array('form','url'));
$this->load->library('form_validation');//验证初始化函数
$this->form_validation->set_rules('username', 'Username', arrary($this->users_model, 'valid_username'));
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
//级联规则
if ($this->form_validation->run()==FALSE)
{
$this->load->view('test/myform');
}
else{
$this->load->view('test/formsuccess');
}
}
public function username_check($str)
{
if($str=='test'){
$this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
return FALSE;
}
else{
return TRUE;
}
}
}
但是不能显示效果,显示页面不存在。