CodeIgniter Forums
Can not Define Callback function in Codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Can not Define Callback function in Codeigniter (/showthread.php?tid=59318)



Can not Define Callback function in Codeigniter - El Forum - 09-20-2013

[eluser]Unknown[/eluser]
Hi, i am using CI 2.1.4
I want use callback in my model, this is code
My callback function
Code:
public function check_state_license_number($license_number){
           if ( preg_match('/^([0-9]{10})$/', $license_number)||preg_match('/^([0-9]{11})$/', $license_number)) {
               return true;
           }else return false;
  }

my config
Code:
$conf = array(
                    array(
                        'field' => 'state_license_number',
                        'label' => 'State License Number',
                        'rules' =>'required|callback_check_state_license_number'
                    )
               );
$this->form_validation->set_rules($conf);

I am sure set validate rules before call run_validation, but it can't validate.
Is CI allow define callback in Model ?


Can not Define Callback function in Codeigniter - El Forum - 09-20-2013

[eluser]noideawhattotypehere[/eluser]
Create file in application/libraries called MY_Form_validation.php
paste this
Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

    function run($module = '', $group = '') {
        (is_object($module)) AND $this->CI = &$module;
        return parent::run($group);
    }

}
should work from now on


Can not Define Callback function in Codeigniter - El Forum - 09-20-2013

[eluser]CroNiX[/eluser]
Callback functions can only be defined in the controller where they are used in the form validation. You can extend the Form_validation library with your own custom rules and then those could be used anywhere. You also need to define the error message for that rule or nothing will be displayed and the user won't know what field didn't pass validation.

/application/libraries/MY_Form_validation.php

Code:
class MY_Form_validation extends CI_Form_validation
{
    function __construct()
    {
parent::__construct();
    }
    
    //Your custom rules below

    function check_state_license_number($license_number)
    {
       //if passes, return true
       if ( preg_match('/^([0-9]{10})$/', $license_number)||preg_match('/^([0-9]{11})$/', $license_number))
       {
           return TRUE;
       }

       //didn't pass, set error message for this rule and return FALSE for fail
       $this->set_message('check_state_license_number', 'The %s field is not a valid license number.');
       return FALSE;
    }
}

Now this is no longer a "callback", but a regular rule. So when setting the validation rules just leave off "callback_", like:
Code:
'rules' =>'required|check_state_license_number'



Can not Define Callback function in Codeigniter - El Forum - 06-26-2014

[eluser]Unknown[/eluser]
Worked fine.. tnx