CodeIgniter Forums
Passing multiple arguments to callback function in form_validation class - 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: Passing multiple arguments to callback function in form_validation class (/showthread.php?tid=29779)



Passing multiple arguments to callback function in form_validation class - El Forum - 04-21-2010

[eluser]Jayakrishnan[/eluser]
Hi,

Is it possible to pass multiple arguments to callback functions for form validation purposes ?
I saw how to pass a single parameter from the following thread.

Passing a dynamic variable as a parameter to a form validation callback function

Can somebody help me out with passing multiple parameters ?

Thanks,
Jayakrishnan GK


Passing multiple arguments to callback function in form_validation class - El Forum - 04-30-2010

[eluser]Jayakrishnan[/eluser]
Any updates ?


Passing multiple arguments to callback function in form_validation class - El Forum - 04-30-2010

[eluser]InsiteFX[/eluser]
Just what are you trying to do?

Give an example and some code.

InsiteFX


Passing multiple arguments to callback function in form_validation class - El Forum - 04-30-2010

[eluser]CroNiX[/eluser]
CI natively only allows for a single parameter. You can either extend CI to allow for more, or use a simple solution that I have been using to get around it. For the 2nd parameter, you can do something like:
Code:
$parameter = 'first_arg' . '||' . 'second_arg' . '||' . 'third_arg';
so your rule would look something like:
Code:
$this->form_validation->set_rules('some_field', 'Some Field Name', 'callback__my_callback_function[first||second||third]');

Then in the callback function:

Code:
function _my_callback_function($field_value, $second_parameter){
    list($first_param, $second_param, $third_param) = split('||', $second_parameter);
    //now you can do something with the params
    if($first_param == 'insert')
    {
        //do something..
    }
}



Passing multiple arguments to callback function in form_validation class - El Forum - 07-25-2013

[eluser]Publisher1[/eluser]
[quote author="CroNiX" date="1272656773"]CI natively only allows for a single parameter. You can either extend CI to allow for more, or use a simple solution that I have been using to get around it. For the 2nd parameter, you can do something like:
Code:
$parameter = 'first_arg' . '||' . 'second_arg' . '||' . 'third_arg';
[/quote]

Hi CroNiX

Thanks a lot for this solution. I tried to realaze it on this way, but that didn't worked for me. Then i tried whout double pipes, i tried with # and + , these two symbols has worked. I am using CI 2.1.3 , maybe this version strips the pipe in the callback funcions.

This solution worked for me :-) :

Code:
required|trim|max_length[50]|xss_clean|callback_userdata_used_and_not_mine[users++username]

Code:
public function userdata_used_and_not_mine($s_value, $s_second_callback_parameter) {
        
    // To use more additional parameters that only 1 additional for callback functions
    $a_second_callback_parameter = explode('++', $s_second_callback_parameter);

    // bla..
}