Friday, December 25, 2015

The way to pass data to external form validation in Codeigniter 2.x.x

External form validation was very important in Codeigniter besides the default form validation since some requirements on programming are above from default settings. Here are the ways on how to set external validation with passing data and return the results. Hope it helps.


Let say I need to pass data from an object retrieved from Codeigniter model as below:

$obj_calendar_data = $this->m_calendar->get_calendar_data_by_calendar_id($calendar_id);
Then I need validate based on value from database as below (in this case value of max_pax):

validation_rules('total_no_of_seat','seat',"required|numeric|callback_check_max_pax[$obj_calendar_data->max_pax]");
 In the same controller, the function for checking max_pax:

function check_max_pax($str,$second_param)
 {
    if( (int) $second_param > (int) $str):
       validation_message('check_max_pax','You can not set the number of pax less than previous number of pax.');
       return FALSE;
        endif;
 }
Note:

check_max_pax function should be the same with the input name in validation_message('check_max_pax');

Thanks to Shahrul for taught me.