am using ci 2.1.4 to create a simple crud application being new to ci and am having this error:'Unable to load the requested class: validation'
here is my controller code:

class Person extends CI_Controller{

    //num of records per page
    private $limit = 10;

    public function __construct(){
        parent::__construct();
        //load library
        $this->load->library(array('table','validation'));
        //load helper
        $this->load->helper('url');
        //load model
        $this->load->model('person_model','',TRUE);
    }

    function index($offset = 0){
        //offset
        $uri_segment = 3;
        $offset = $this->uri->segment(uri_segment);
        //load data
        $person = $this->person_model->get_paged_list($this->limit, $offset)->result();
        //generate pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('person/index/');
        $config['total_rows'] = $this->person_model->count-all();
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        //generate table data
        $this->load->library('table');
        $this->table->set_empty(" ");
        $this->table->set_heading('No', 'Name', 'Date of Birth (dd-mm-yyyy)', 'Sex', 'Actions');
        $i = 0 + $offset;
        foreach ($persons as $person){
            $this->table->add_row(++$i, $person->name, strtoupper($person->sex)=='M'? 'Male':'Female', date('d-m-Y', strtotime($person->dob)),
            anchor('person/view/'.$person->id,'view',array('class'=>'view')).' '.   
            anchor('person/update/'.$person->id,'update',array('class'=>'update')).' '.
            anchor('person/delete/'.$person->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure you want to delete this person?')"))
            );
        }
        $data['table'] = $this->table->generate();
        //load view
        $this->load->view('person_list', $data);
    }

Any idea what might be wrong?

Dani AI

Generated

@murugami eric The message is raised because CI 2.x no longer ships the legacy Validation class, so the loader cannot find validation. @Ajay Gokhale and are on point: switch to the Form Validation library and reference it as $this->form_validation. Also check application/config/autoload.php: if you previously autoloaded validation, remove it; if you want it autoloaded, use form_validation instead.

A minimal pattern for using Form Validation looks like this:

// Load once (or autoload it)
$this->load->library('form_validation');

// Define rules in the action that handles the form submit
$this->form_validation->set_rules('name', 'Name', 'required|min_length[2]');
$this->form_validation->set_rules('dob', 'Date of Birth', 'required');

if ($this->form_validation->run()) {
    // Save via model, then redirect
} else {
    // Reload the form view; display errors with validation_errors()
}

While you are here, a few small gotchas in your snippet that will likely bite next:

  • Use the variable when reading the URI segment: $this->uri->segment($uri_segment) (note the leading $).
  • Keep your variable names consistent: if you assign to $persons = ...->result();, iterate foreach ($persons as $person).
  • Ensure your model method call is valid PHP: count_all() (underscores), not count-all() (a minus will cause a parse error).

Once you replace validation with form_validation everywhere (including autoload, controller loads, and method calls), the class will load and your rules will run as expected.

Recommended Answers

All 4 Replies

Hi,

The error says that library file is missing. I think 'validation' is not CI core library. I think you need 'form_validation'. So please try with 'form_validation' instead of 'validation'.

Please check and let me know.
Thanks,
Ajay

I totally agree with Ajay, the CI library for validating form is called form_validation.

Hi,

After changing to 'form_validation' is it working?

Please let me know.
Thanks,
Ajay

This has nothing to do with the controller code but the configurations in autoload.php. Go to the config folder and open autoload.php go the line where its written $autoload['libraries'] = array('.... then remove validation from the list and it work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.