Welcome Guest, Not a member yet? Register   Sign In
Aaargh!! What Am I Doing Wrong With This Callback?
#1

[eluser]Phil Norton[/eluser]
Right, I've done callbacks before with no problems, but what should be very simple is taking me an age to get working. Perhaps it's a case of looking at the same thing for too long, so hopefully someone out here can spot what I'm doing wrong!!

In my Controller:
Code:
$rules['start_time'] = "required|callback_timeCheck";

And then the validation in the same Controller:
Code:
function timeCheck($time) {
if (!ereg("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$", $time)) {
        $this->validation->set_message('timeCheck', 'Please Enter All Times In 24-Hour Format eg 09:00');
        return FALSE;        
    } else {
        return TRUE;
    }
}

If I leave the field blank, CI's "required" validation works and throws up an error. But if I type anything in the box, the error isn't being triggered. However, if I use the regex to check the string entered by doing this when I submit the form, I get the correct result so I know the regex is correct:
Code:
if (!ereg("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$", $this->input->post('start_time'))) {
    echo "INCORRECT FORMAT";
} else {
    echo "CORRECT FORMAT";
}



Like I said, it's possible that I've been staring at it for so long now that the very obvious answer is laughing at me whilst staying hidden from my eyes!!

Any help would be gladly appreciated!

Phil
#2

[eluser]Yash[/eluser]
change your function name I mean add _ like I've showed it.. A idea only
Code:
function _timeCheck($time) {
if (!ereg("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$", $time)) {
        $this->validation->set_message('timeCheck', 'Please Enter All Times In 24-Hour Format eg 09:00');
        return FALSE;        
    } else {
        return TRUE;
    }
}
#3

[eluser]Phil Norton[/eluser]
Nope, that doesn't work either. Anyone else?
#4

[eluser]Yash[/eluser]
Try to echo $time if its coming check your if statement
#5

[eluser]Phil Norton[/eluser]
I tried that to begin with in the callback:

Code:
function timeCheck($time) {
echo "TESTING, TESTING, ONE, TWO, THREE!!";
if (!ereg("^([0-1][0-9]|[2][0-3]):([0-5][0-9])$", $time)) {
        $this->validation->set_message('timeCheck', 'Please Enter All Times In 24-Hour Format eg 09:00');
        return FALSE;        
    } else {
        return TRUE;
    }
}

But when I post data to the form, it's not echoing the "TESTING" statement at all, whether or not I'm putting a time in the correct format. Am I calling the callback correctly? Something must be wrong if it's not even echoing that statement, right?

Code:
$rules['start_time'] = "required|callback_ timeCheck";

This is driving me crazy! ;o)
#6

[eluser]Phil Norton[/eluser]
D'oh! What a stupid mistake, basically, I'd wrongly nested the callback functions within another function in the controller, which is why they weren't working. Once I'd moved them, they worked perfectly!!

Sorry for any inconvenience caused, and thanks for looking at it for me!

Phil




Theme © iAndrew 2016 - Forum software by © MyBB