CodeIgniter Forums
form validation callbacks aren't being called - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: form validation callbacks aren't being called (/thread-26864.html)

Pages: 1 2


form validation callbacks aren't being called - El Forum - 01-25-2010

[eluser]darkhouse[/eluser]
Hey guys,

I have a weird issue that, in my 2 years of using CodeIgniter, I've never seen. My form validation callbacks are not being called... at all. Here's my code.

Code:
function search(){
    $rules = array(
        array('field'=>'check_in', 'label'=>'Check In', 'rules'=>'trim|required|xss_clean|callback__valid_check_in')
    );
    $this->form_validation->set_rules($rules);
    if($this->form_validation->run()){
        echo 'success';
    } else {
        echo validation_errors();
    }
}
    
function _valid_check_in($date){
    $this->form_validation->set_message('_valid_check_in', 'Is check in callback even being called?');
    return FALSE;
}

As you can see, I've placed a validation message at the beginning of the callback just to see if it's being called, then I return FALSE. When I run the form, it should just give me that validation error... but it doesn't. I get the 'success' output, signifying the form was validated properly but we know it's not because the callback is set to return FALSE every time... so that means the callback isn't being called.

If I add the valid_email rule to the check_in, the form fails (as it should because this is a date field) and gives me the "Must be a valid email" message. But I still don't get the callback error.

I've tried changing the functions to public instead of private... didn't help. I've tried running it locally as well as on our development server... same issue.

One other thing I did was go into the Form_validation library, into the _execute function and changed this:

Code:
if ( ! method_exists($this->CI, $rule))
                {    
                    continue;
                }

to this:

Code:
if ( ! method_exists($this->CI, $rule))
                {    
                    echo '<pre>', print_r(get_class_methods($this->CI), true), '</pre>';
                    die($rule.' does not exist.');
                    continue;
                }

I get this:

Code:
Array
(
    [0] => __construct
    [1] => CI_Base
    [2] => get_instance
)

_valid_check_in does not exist.

So, it makes sense that my callback wouldn't be called if it doesn't exist... but why doesn't it exist?

I've used callbacks in the past, many times, and never had an issue (aside from my own little bugs). I've been through the documentation multiple times, thinking maybe I overlooked something. Is there something I'm missing? Has anyone ever had this issue before? I've searched the forums, but can't seem to find anybody having the same problem, and that worries me.

Thanks.


form validation callbacks aren't being called - El Forum - 01-25-2010

[eluser]darkhouse[/eluser]
I tried a brand new version of CI, and that still didn't help. I ended up changing my code to this, which I know is a little silly, but it works and allows me to move on, though I still want this callback issue figured out... so weird.

I have my own message system, so I just used that inside the callback (or what used to be a callback) and now I run it after the validation runs, along with a success variable that I set to FALSE if something happens. If it's still TRUE by the time it gets to the end, then I display the success message.

Code:
function search(){
    $rules = array(
        array('field'=>'check_in', 'label'=>'Check In', 'rules'=>'trim|required|xss_clean')
    );
    $this->form_validation->set_rules($rules);
    if($this->form_validation->run()){
        $success = TRUE;
        
        if(!$this->_valid_check_in($this->input->post('check_in'))) $success = FALSE;
            
        if($success){
            $this->message->set('Success', 'success');
        }
    } else {
        $this->message->set(validation_errors(), 'error');
    }
    $this->load->view('book_room');
}
    
function _valid_check_in($date){
    if($date < date('Y-m-d')){
        $this->message->set('Check in date must be today or later.', 'error');
        return FALSE;
    }
    if($date > date('Y-m-d', time()+60*60*24*365.25)){
        $this->message->set('Check in date too far in advance.', 'error');
        return FALSE;
    }
    return TRUE;
}

But if anyone else has any insight into my initial callback issue, I'd really appreciate it. Thanks.


form validation callbacks aren't being called - El Forum - 01-26-2010

[eluser]flaky[/eluser]
are you using hmvc ?


form validation callbacks aren't being called - El Forum - 01-26-2010

[eluser]darkhouse[/eluser]
Yes I am. I never thought that would affect it. Is there a workaround?


form validation callbacks aren't being called - El Forum - 01-26-2010

[eluser]flaky[/eluser]
you should extend Form validation
Code:
class MY_Form_validation extends CI_Form_validation{
    
    function run($module = '', $group = ''){
        (is_object($module)) AND $this->CI = &$module;
            return parent::run($group);
    }
    
}

and when using form validation insert $this into run()
Code:
if($this->form_validation->run($this))
.
.
.



form validation callbacks aren't being called - El Forum - 01-26-2010

[eluser]darkhouse[/eluser]
Thanks, I'll try that.


form validation callbacks aren't being called - El Forum - 02-19-2010

[eluser]darkhouse[/eluser]
I know it's been a while, but I just wanted to mention that this worked perfectly. Thanks!


form validation callbacks aren't being called - El Forum - 04-20-2010

[eluser]Unknown[/eluser]
Thanks @flaky for clairity on the hmvc example, I was considering that route for another project.

For those not using it, Form_validation only .: substr(0-9) or "callback_" on rules;

Usage ============
function _user_name_exists($str)
{
$this->form_validation->set_message('_user_name_exists', 'The user name is already in use. Please try again.');
....
....
}
Usage ============
ex. validation string : 'trim|required|min_length[2]|callback__user_name_exists'
- notice the double underscore.

At first I too thought this was an anomaly; yet if one follows through form_validation.php its clear.

~nolo


form validation callbacks aren't being called - El Forum - 04-26-2010

[eluser]Unknown[/eluser]
@flaky: Thanks, its worked perfectly!


form validation callbacks aren't being called - El Forum - 05-28-2010

[eluser]goliatone[/eluser]
I was going nuts with this issue!
Wasted a whole two hours, thanks @flaky for the info.
It worked great.