如何在 codeigniter 中显示错误消息?

How to display the error message in codeigniter?

验证有效,但错误消息未显示在我的视图中。如何修复视图?请帮忙谢谢:)

这是模型。

function entry_insert(){
    $this->load->library('form_validation');
    $this->load->database();
    $this->form_validation->set_rules('name','Name','required|is_unique[burrower.name]');
    if($this->form_validation->run()== FALSE){
        $data = array(  
                  'name'=>$this->input->post('name'),
                  'address'=>$this->input->post('address'),
                  'age'=>$this->input->post('age'),
                );
        $this->db->insert('burrower',$data);
    }else{
        redirect('burrower/input');
    }
 }

这是挖洞者的视角。 抱歉拼错了

<div class='container'>
  <?php echo validation_errors(); ?>
</div>

当您使用 if($this->form_validation->run()== FALSE){ it needs to contin your fail part 时。不是成功。

if ($this->form_validation->run() == FALSE){
    # Fail
    $this->load->view('burrower/input');
}
else{
    # Success

    $data = array(  
      'name'    => $this->input->post('name'),
      'address' => $this->input->post('address'),
      'age'     => $this->input->post('age'),
    );
    $this->db->insert('burrower', $data);

    $this->load->view('success_view');
}

阅读 Form Validation Tutorial - Codeigniter.com

您还必须设置显示验证错误的错误消息。要设置错误消息,请使用 $this->form_validation->set_message('required', '%s is required.');

注意:你的逻辑也不对。如果 if($this->form_validation->run()== FALSE) 这部分 运行s 你必须显示错误,在其他部分你应该 运行 你的插入查询。