只需在模型中创建一个数组:
$validate = array(
array( 'field' => 'username', 'label' => 'Username', 'rules' => 'required,trim' ),
array( 'field' => 'password', 'label' => 'Password', 'rules' => 'required|min_length[8]' )
);
$data=array(
'username'=>'admin',
'password'=>'123456',
);
然后在save()之前实现可用于验证数据的方法
function validate($data) {
if (!empty($this->validate)) {
foreach ($data as $key => $value) {
$_POST[$key] = $value; }
$this->load->library('form_validation');
$this->form_validation->set_rules($this->validate);
return $this->form_validation->run(); }
else
{
return TRUE;
}
}
这样就可以获取到$_POST过来的数据,然后进行测试