Like us on Facebook and stand a chance to win pen drives!

Passing Multiple Parameters to Form Validation Custom Callbacks in Codeigniter




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


$this->form_validation->set_rules
('hstate', 'State', 'required|callback_suburb_check[' . $suburb . ']');

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.


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

Then in your callback,


function _my_callback_function($field_value, $second_parameter){
    list($first_param, $second_param, $third_param) = split('||', $second_parameter);
    ...
}

0 comments:

How to check HTML5 Web Storage Support in JavaScript?

HTML5 Data Storage
HTML5 Data Storage



function supports_html5_storage() {
                try {
                    return 'sessionStorage' in window && window['sessionStorage'] !== null;
                } catch (e) {
                    return false;
                }
            }
            alert(supports_html5_storage());

0 comments:

Copyright © 2012 The Code Junction.