Welcome Guest, Not a member yet? Register   Sign In
Private callback functions
#1

[eluser]m40[/eluser]
CI's validation system supports callback functions. Is it possible to use private functions as callback functions?
#2

[eluser]bobbob[/eluser]
What do you mean by private?
#3

[eluser]m40[/eluser]
Basically, a private function is a function that is available only to the class that declares or defines it or to subclasses of that class.
#4

[eluser]Dam1an[/eluser]
Well, just try it and find out
It won;t officially be documented anywhere, as Ci is PHP4, which doesn't support private or protected functions
#5

[eluser]fireproofsocks[/eluser]
I was wondering the same thing. Adding "protected" or "private" to any of your controller's functions generates an error. My main concern was that by adding my own callback functions to my controller, I would be adding more URI, which I didn't want. You can use the underscore trick: any function whose name begins with an underscore is not considered a valid URI, so your validation rule might look like this:

Code:
// notice the two underscores following 'callback'
$this->form_validation->set_rules('something', 'Something', 'callback__custom_validate');

// [...]

function _custom_validate($str)
{
// ...
}
#6

[eluser]pistolPete[/eluser]
You could also extend the form_validation class and add your validation functions there.
#7

[eluser]fireproofsocks[/eluser]
Yeah, extending the Form_validation class is definitely a better way to go. That way you can even have custom validation rules that take an additional parameter, and you can reuse all your rules.
#8

[eluser]CISCK[/eluser]
[Edit] I realize my question didn't make much sense, but I figured out the problem. I didn't include the $rules arguments in the constructor:

Code:
class MY_Form_Validation extends CI_Form_validation {

    function __construct($rules = array())
    {
        parent::CI_Form_validation($rules);
        log_message('debug', 'MY_Form_Validation Class Initialized');
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB