简体   繁体   中英

How to display the error message in codeigniter?

The validation is working but the error message doesn't display on my view. How to fix the view? Please help thanks:)

This is the model.

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');
    }
 }

This is the view for burrower. sorry for the wrong spelling

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

When you use if($this->form_validation->run()== FALSE){ it needs to contin your fail part . Not success.

Ex

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');
}

Read Form Validation Tutorial - Codeigniter.com

You also have to set the error message for showing the validation error. To set the error message, use $this->form_validation->set_message('required', '%s is required.'); .

Note: Your logic is also wrong. if if($this->form_validation->run()== FALSE) this part runs the you have to show the error and in else part you should run your insert query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM