我正在尝试验证我的数据库中是否存在现有的job_code,当我插入一些内容但我得到了这个
无法访问与您的字段名称对应的错误消息 JOB_CODE。
这是我的代码。
控制器
public function create(){
$this->load->library('form_validation');
$this->form_validation->set_rules('JOB_CODE','JOB_CODE','required|trim|callback_if_exist');
$this->form_validation->set_rules('JOB_NAME','Job Name','required|trim');
if($this->form_validation->run()){
$this->load->model('Job_Titles_Model');
$input = array(
'JOB_CODE' => $this->input->post('JOB_CODE'),
'JOB_NAME' => $this->input->post('JOB_NAME')
);
$this->Job_Titles_Model->insert($input);
$this->add_view();
}else{
$this->add_view();
}
}
public function if_exist(){
$this->load->library('form_validation');
$this->load->model('Job_Titles_Model');
if($this->Job_Titles_Model->validate_code()){
return true;
}else{
$this->form_validation->set_message('if_exist','Job code already exists.');
return false;
}
}
模型
public function validate_code(){
$sql = "SELECT * FROM job_titles WHERE JOB_CODE =?";
$data = array('JOB_CODE' => $this->input->post('JOB_CODE'));
$query = $this->db->query($sql, $data);
if($query->num_rows() == 0){
return true;
}else{
return false;
}
视图
<?php
echo form_open('Job_Titles/create');
//echo "Job Title Code:";
//echo form_input('JOB_CODE','', 'class ="field"');
//echo "Job Title:";
//echo form_input('JOB_TITLE','', 'class ="field"');
?>
<p>
<label class="field" for="JOB_CODE"><span>*</span>Job Code:</label>
<input type = "text" name="JOB_CODE" class ="textbox-300">
</p>
<p>
<label class="field" for="JOB_NAME"><span>*</span>Job Name:</label>
<input type = "text" name="JOB_NAME" class ="textbox-300">
</p>
<?php
echo form_submit('submit','Save');
echo validation_errors();
echo form_close();
?>
答案 0 :(得分:0)
$errs=array('password_check'=>'nice message');
$this->form_validation->set_rules('password','password','callback_password_check',$errs);
似乎需要一个具有函数名称的数组作为字段名称。 这是codeigniter 3.1.2