phpcodeignitervalidation

Autoloading form_validation library


I am having problem autoloading the form_validation library in Codeigniter. I have a controller Posts with a create function which works great.

public function create(){
       $data['title']='New Post';

              $this->load->view('templates/header');
              $this->load->view('posts/create', $data);
              $this->load->view('templates/footer');   
}

Now I want to do form validation . Something like this :

public function create(){
       $data['title']='New Post';
       $this->form_validation->set_rules('title', 'Title','trim|required|min_length[5]|max_length[128]');
       $this->form_validation->set_rules('body', 'Blog','trim|required|min_length[5]');
       if($this->form_validation->run==FALSE){
              $data['errors']=validation_errors();
              $this->load->view('templates/header');
              $this->load->view('posts/create', $data);
              $this->load->view('templates/footer');   
       }else {
              $this->load->view('templates/header');
              $this->load->view('posts/success', $data);
              $this->load->view('templates/footer'); 
       }
}

For now I am not calling any model to store the data, just showing the success message by loading posts/success view . However , even before I code the create function with the validation(that is the above code) , the moment I add form_validation in the autoload.php (even with the first code) like so :

$autoload['libraries'] = array('database','form_validation');

I am getting the below error :

Message: Undefined property: Post_model::$load

Filename: libraries/Form_validation.php

Line Number: 147

Backtrace:

File: C:\xampp\htdocs\ciblog\index.php Line: 292 Function: require_once

I do not understand the error as I am not even using the Post_model in the method .

My Post_model.php is :

class Post_model extends CI_Controller {
public function get_post($slug=NULL){
   if(!$slug) {
     $query = $this->db->get('posts');  
     return  $query->result();

  } else {
     $this->db->where('slug',$slug);
     $query = $this->db->get("posts");
     return $query->result();
  }
 } 
}

My complete post controller is this :

<?php
class Posts extends CI_Controller {
public function index(){
    $data['title']='Latest Posts';
    $posts=$this->post_model->get_post();
    $data['posts']=$posts;
    $this->load->view('templates/header');
    $this->load->view('posts/index',$data);
    $this->load->view('templates/footer');
}
public function view($slug){
    $posts=$this->post_model->get_post($slug);
    if(empty($posts)){
        show_404();
    } else {
       $data['posts']=$posts;
       $this->load->view('templates/header');
       $this->load->view('posts/view',$data);
       $this->load->view('/templates/footer');
    }   
}
public function create(){
       $data['title']='New Post';
       $this->form_validation->set_rules('title', 'Title','trim|required|min_length[5]|max_length[128]');
       $this->form_validation->set_rules('body', 'Blog','trim|required|min_length[5]');
       if($this->form_validation->run()==FALSE){
              $data['errors']=validation_errors();
              $this->load->view('templates/header');
              $this->load->view('posts/create', $data);
              $this->load->view('templates/footer');   
       }else {
              $this->load->view('templates/header');
              $this->load->view('posts/success', $data);
              $this->load->view('templates/footer'); 
       }
   }
}

I have autoladed the model which is working .I searched other posts , most were to do with the first letter of the model name being not in caps which is not my case . Could somebody please make me understand whats going wrong ?


Solution

  • You used your model in your controller without loading it, you have to load it first:

    $this->load->model('post_model');
    

    Another error here is that model extends CI_Model:

    class Post_model extends CI_Model