我有一个带有一个输入字段名称的表单=“uid”type =“text” 表'tbl_users'并且有一个列'uid' 我想验证$ this-> input-> post('uid'),如果它已经在数据库列中。代码如下: -
控制器/ File1.php --in validate_data()
$this->load->model('model1');
$data['validation_result'] = $this->model1->validate_data();
$this->load->view('view1');
型号/ model1.php
public $set_of_rules = [
'item1'=>[
'field'=>'uid',
'label'=>'User ID',
'rules'=>'trim|required|callback_validateUID',
'field'=>'uid',
'errors'=>[
'required'=>'uid must be filled in',
'validateUID'=>'UID does not exist, please input valid UID',
],
],
];
public validate_data()
{
$this->form_validation->set_rules($this->set_of_rules);
if(!$this->form_validation->run())
return validation_errors() ;
}
else
{
/*$this->db->do_some_database_tasks */
return 'success' ;
}
public validateUID()
{
var_dump('UID validation started...');
$uid = $this->input->post('uid');
$result = $this->db->where(['uid'=>$uid])->get('tbl_users')->row();
if($result!=NULL) return TRUE;
else return FALSE;
}
问题是,验证规则'callback_validateUID'没有执行[因为我已经在其中放入了一个脚本,以便我可以理解验证运行了回调函数]但验证错误消息('UID不存在,请显示输入有效的UID'),即使我将输入字段留空,“必需”也不是检查!
有人可以帮忙吗?
答案 0 :(得分:0)
在模型中尝试这个
function SetofRules() {
$rules = array(
array('field'=>'uid', 'label'=>'User', 'rules'=>'trim|required'),
array('field'=>'password', 'label'=>'Password', 'rules'=>'trim|required'),
);
$this->load->library('form_validation');
$this->form_validation->set_rules($rules);
$errors = array();
if ($this->form_validation->run() == FALSE)
foreach ($rules as $r)
$errors[$r['field']] = $this->form_validation->error($r['field']);
if (!$errors['uid'])
if ($this->userPrefix( $this->input->post('uid'), 'uid'))
$error['uid'] = $this->form_validation->error('uid');
if (!empty($errors)) return $errors;
else return false;
}