Monday, June 30, 2014

Passing Multiple Parameters to Form Validation Custom Callbacks in Codeigniter

[caption id="attachment_979" align="aligncenter" width="225"]Codeigniter Custom Callbacks Codeigniter Custom Callbacks[/caption]

CI natively only allows for  single parameter in custom callbacks. For an instance see the following code.

[sourcecode language="php"]
$this->form_validation->set_rules
('hstate', 'State', 'required|callback_suburb_check[' . $suburb . ']');
[/sourcecode]

If you need to pass multiple parameters, you have several options. Obviously you can change CI behaviour by subclassing the core library. But in this tutorial we follow the less pain approach. That 's to access required parameters via POST variables within your custom callback function. They are still available in this scope.

There is another way using your PHP string handling knowledge. Just formatting all the parameters as single string and passing to the callback function.

[sourcecode language="php"]
$parameters = 'first_arg' . '||' . 'second_arg' . '||' . 'third_arg';
$this->form_validation->set_rules
('some_field', 'Some Field Name', 'callback__my_callback_function['.$parameters.']'); 

[/sourcecode]

Then in your callback,

[sourcecode language="php"]
function _my_callback_function($field_value, $second_parameter){
    list($first_param, $second_param, $third_param) = split('||', $second_parameter);
    ...
}
[/sourcecode]

No comments:

Post a Comment

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...