Monday, October 21, 2013

Display form validation error count in codeigniter V2

The following are the steps for showing the form validation error counts in codeigniter.
Step 1: Create a new file named MY_Form_validation.php under /application/libraries/. Remmeber to Ensure the following line is present in config.php
$config['subclass_prefix'] = 'MY_';

Step 2: Place the following code in the newly created MY_Form_validation.php:

class MY_Form_validation extends CI_Form_validation {

    public function __construct( $rules=array() )
    {
        parent::__construct($rules);
    }

    /*   This function will return the error count    */    
    function get_error_count()
    {
       return count($this->_error_array); 
    }
}

You can find more details about the core form validation class from here
Now you will be able to get the validation error count via the following code: 
$this->load->library('form_validation');
$this->form_validation->get_error_count(); 

No comments:

Post a Comment