EllisLab text mark
Advanced Search
     
form validation callbacks aren’t being called
Posted: 25 January 2010 02:50 PM
Avatar
Joined: 2008-08-19
340 posts

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.

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:

if ( ! method_exists($this->CI$rule))
                
{     
                    
continue;
                

to this:

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:

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.

 
Posted: 25 January 2010 03:37 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-19
340 posts

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.

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.

 
Posted: 26 January 2010 04:41 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2009-11-01
296 posts

are you using hmvc ?

 Signature 

http://flakron.net

 
Posted: 26 January 2010 07:51 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-19
340 posts

Yes I am.  I never thought that would affect it.  Is there a workaround?

 
Posted: 26 January 2010 08:00 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2009-11-01
296 posts

you should extend Form validation

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()

if($this->form_validation->run($this))
.
.
 Signature 

http://flakron.net

 
Posted: 26 January 2010 06:00 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-19
340 posts

Thanks, I’ll try that.

 
Posted: 19 February 2010 11:05 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-19
340 posts

I know it’s been a while, but I just wanted to mention that this worked perfectly.  Thanks!

 
Posted: 20 April 2010 08:56 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2010-03-24
1 posts

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

 Signature 

We may only reverse engineer the future.. for now.
CI became my php vanilla kit of choice by trial and error. Strip it, Compile it, Kode it. Done.

 
Posted: 26 April 2010 05:22 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2008-11-26
1 posts

@flaky: Thanks, its worked perfectly!

 
Posted: 28 May 2010 02:06 PM   [ # 9 ]   [ Rating: 0 ]
Joined: 2007-06-06
22 posts

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

 Signature 

DiseƱo Web || Desarrollo Flash

 
Posted: 04 July 2010 04:15 PM   [ # 10 ]   [ Rating: 0 ]
Joined: 2007-07-16
34 posts

thank you flaky!

Strange that the callbacks was worked on other project of mine, but not this time.

 
Posted: 13 July 2010 10:37 AM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2009-08-03
4 posts
flaky - 26 January 2010 01:00 PM

you should extend Form validation

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()

if($this->form_validation->run($this))
.
.

i’ve tried to use your suggestion but my form still not working.. is there any server or php level requirement to run this hmvc ?

my php version is : PHP Version 5.2.13
mysql version is : MySQL client version: 4.1.22

i’ve tried to install fresh ci and insert simple callback vallidation but it seems not working..

 
Posted: 15 August 2010 05:18 PM   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2008-06-24
26 posts

same here..

 Signature 

EckoSession - another native Session library

 
Posted: 26 March 2011 07:18 AM   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2010-02-09
227 posts

Yeah, I’m using Modular Extensions and this technique isn’t working for me, either.

 
Posted: 18 April 2012 04:19 PM   [ # 14 ]   [ Rating: 0 ]
Joined: 2011-06-24
101 posts

Anyone found a solution to this yet? I can’t get it working.

 
Posted: 17 October 2012 04:53 PM   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2012-09-12
1 posts

That worked fine for me. Thanks