Welcome Guest, Not a member yet? Register   Sign In
callback feature on form validation library

The 'callback' feature on the form validation library does not seem to work.

I have tried everything but the below won't work. Can someone please help? Sad

class Myform extends MY_Controller {

    function Myform()
        $this->load->helper(array('form', 'url'));

    function index() {    
        $this->form_validation->set_rules('datecreated', 'datecreated', 'callback_date_check');
        $this->form_validation->set_error_delimiters('<li>', '</li>');

        if ($this->form_validation->run() == FALSE) {
            $this->display('upload/index', $data);


    function date_check($str) {        
        echo "functionm called";

There seems to be a bug in the form_validation library when calling a callback function from within an application library (ie library stored in application/libraries folder). The form validator extracts the callback method correctly but fails to call it. From what I can tell the _execute method fails to find the function when calling method_exists at around line 593.
Adding the class name to the beginning of the method doesn't work (ie rule of 'callback_mylibrary->callback_function').

[eluser]Colin Williams[/eluser]
I think this problem has come up before. Try adding the 'required' rule and then see if it works.

These are the rules I have: required|trim|xss_clean|callback_check_username

The last one is in a library, which is where the form validator is called from. It appears to be failing as it gets to the rule checking stage then when it checks to see if the callback function exists it returns false and doesn't execute the function.

[eluser]Colin Williams[/eluser]
Well, that's because it's not looking in the library, it's looking in the controller.

The form validator is called from within the same library as the callback function. Is there any way to get it to call the function in the library?

[eluser]Colin Williams[/eluser]
You'd have to rewrite the Form_validation class to work in that way. The Form_validation library expects you callbacks to be a method of the CI superobject, i.e. the controller. It tries $CI->$callback(). That's the convention. CI couldn't magically know from which library the validation object was being called (unless there is some sick PHP functionality I've yet to hear of Smile). When you want to subvert conventions, you need to update the core functionality. One thing you could do is extend the validation class and provide custom validation methods (like in a MY_Form_validation.php library).

Has anyone been able to fix this issue?

[eluser]Colin Williams[/eluser]
Try reading through the thread. It's not an issue that needs fixing. The form validation class calls methods of the $CI superobject, i.e. the controller. It can't tell from which library it is being called.

[quote author="Colin Williams" date="1230435159"]One thing you could do is extend the validation class and provide custom validation methods (like in a MY_Form_validation.php library).[/quote]

I've got this working through a pass-through function in the controller, but would like to call MY_Form_validation library functions directly from the set_rules function. If that's possible, can somebody point out the syntax? (my OOP skills are pretty weak and $this->form_validation->function_name doesn't seem to work)

If it's not possible (or not possible without modifying the core form validation library), can somebody explain why?


EDIT: I apologize that this is in the Bug Report forum... just following up on an existing thread

Theme © iAndrew 2016 - Forum software by © MyBB