Welcome Guest, Not a member yet? Register   Sign In
Problems with Callback Functions and Validation Class
#1

[eluser]mariowarner[/eluser]
Hi guys,

I've ran out of time, spent about 5 hours reading, debugging, reading, debugging but still haven't found any solution to my problem. I've used the code as stated on the user_guide about creating a callback function for a form validator but it just won't work on my server (although it did work on my local machine).

If you guys can try and check out the website [ http://www.floralstyle.biz/ ], I have a pre-launch registration form which accepts an email address (simple) and a captcha input. My validation code looks like this:

Code:
$rules['cinput'] = "required|callback_cinputCheck";
$rules['email'] = "required|valid_email";
$rules['confirm_email'] = "required|valid_email|matches[email]";


No problems with the email and confirm email code. I only have problems with the callback_cinputCheck. Below is the cinputCheck function:

Code:
function cinputCheck($str)
    {
        if ($str != $_SESSION["pass"]) // the correct value of captcha -> this validates ok,
                             // cos it doesn't proceed with the signup process
                              // successfully if you don't type the correct captcha.
        {
            $this->validation->set_message('cinputCheck', 'Incorrect Captcha characters.');
            return FALSE;                            
        }
        else
        {
            return TRUE;        
        }
    }

It's basically the same as the one on the user_guide's Validation Class (callback function). I just can't see why I keep getting the following error message when testing it online (while I don't get the same message offline):

Code:
Error(s):
Unable to access an error message corresponding to your field name.

Does it have to do with server configurations? Cos I did upload all the CI files in a zip file and extracted it on the server to avoid BINARY/ASCII file upload problems.

Any help will be greatly appreciated! Smile
#2

[eluser]Michael Ekoka[/eluser]
Try setting the field's name for cinput in the validation:
Code:
$fields['cinput'] = '';
$this->validation->set_fields($fields);

If that doesn't work, could you tell us what version of CI you're using?
#3

[eluser]mariowarner[/eluser]
Thanks for the reply! Smile I'll try it now and tell you what happens.
#4

[eluser]mariowarner[/eluser]
I'm still getting the same error. I'm using version 1.6.1. Do you think it's a bug? It does work on my machine (OS X 10.4.11 running Apache 1.3.33 and PHP 5.2.4. Does this have to do with the PHP version? I just checked my server's version and it's PHP 4.3.11.
#5

[eluser]Michael Ekoka[/eluser]
I'm suspecting a problem with discrepancies between the case insensitive way php4 handles function names and the case sensitive hash values. Try renaming the function from cinputCheck to cinput_check and change the validation accordingly:
Code:
$rules['cinput'] = "required|callback_cinput_check";

...

$this->validation->set_message('cinput_check', 'Incorrect Captcha characters.');
#6

[eluser]mariowarner[/eluser]
thanks. i did something similar to that before i renamed the function into cinputCheck. i named it captcha_check before. i'll try and do it again now, though, since i've finished with all the rest of the codes for the pre-launch phase - gives me time to check back on unsolvable problems (right now, just that part).

coding in CI is really fast. i spent only about 3 hours from writing the codes for adding email addresses to the database, creating email template, sending confirmation email, creating the cancellation of subscription function. and that's all while reading the user_guide while working step-by-step. Smile

'nuf said, i'll revise the code again and post the results here.
#7

[eluser]mariowarner[/eluser]
hmm.. still didn't work. maybe i should change the way i handle the session variable into CI's way.
#8

[eluser]Michael Ekoka[/eluser]
A little hack to help you for now:
Code:
function cinputCheck($str)
    {
        if ($str != $_SESSION["pass"]) // the correct value of captcha -> this validates ok,
                             // cos it doesn't proceed with the signup process
                              // successfully if you don't type the correct captcha.
        {
            $this->validation->_error_messages['cinputCheck'] = 'Incorrect Captcha characters.';
            return FALSE;                            
        }
        else
        {
            return TRUE;        
        }
    }
#9

[eluser]mariowarner[/eluser]
Thanks, man!

It still won't work so I did a different hack.

I retained the original format of the callback function because it works on my local machine (in case the issue is not with CI but on the server configurations:
Code:
function cinputCheck($str)
    {
        if ($str != $_SESSION["pass"])  {
            $this->validation->set_message('cinputCheck', 'Incorrect Captcha characters.');
            return FALSE;                            
        }
        else
        {
            return TRUE;        
        }
    }

And then I did a string search for the error message generated by Validation Class in case it can't find an error message (this only works well with my codes because I know for sure that there's only one error that generates that error message):
Code:
// set validation rules
        $rules['cinput'] = "required|callback_cinput_check";
        $rules['email1']     = "required|valid_email";
        $rules['email2']     = "required|valid_email|matches[email1]";        

        $this->validation->set_rules($rules);
        
        if ($this->validation->run() == FALSE) {
            $data['success'] = "0";
            
            if($this->validation->error_string) {
                $data['message'] .= '<br />Error(s): ';
            }
                    
            $data['message'] .= $this->validation->error_string;
            
            $reg_ex = 'Unable to access an error message corresponding to your field name.';
            $str = $data['message'];
            
            // i only used this because i know for sure that the $reg_ex message only shows up
            // when i get to the Captcha callback function.
            if (strpos($str, $reg_ex) !== false) {
               $data['message'] = str_replace('Unable to access an error message
                                       corresponding to your field name.',
                    'The characters you typed did not match the characters on the box.', $str);
            }
            
            $this->load->view('prelaunch', $data);
        }
        
        else {
                         // no more errors, proceed with the success methods
                }

Again, it might not work well with bigger forms with more than one callback function (cos i think the error has to do with Validation Class' callback functions), but it did the trick for now.
#10

[eluser]maxez[/eluser]
I am new to CodeIgniter. I spent a day playing with both Cakephp and codeigniter, and decided for the former largely due to their validation flexibility. Now I am starting to unfortunately regret my decision!

The validation class have several flaws that are only found when you run your code on PHP 4. Unfortunately I spent all my time coding on my PHP5 environment, only to find all kinds of problems when I built the app on my client's server running PHP4.

The problem described above is just one of many problems found when moving the code to PHP4. I wish the code base would be fully tested on both PHP4 and PHP5 before claiming compatibility with both versions. Unfortunately, I cannot use that "hack" since I have TWO call back functions that are having the same problem. In addition, it is a horrible hack Smile (I mean, my kudos to you for coming up with a solution, but it is still ugly Smile ).

I am at a loss on how to fix this short of having to get my hands dirty and try to "hack" the validation class myself (which will take a LONG time since I am not familiar with the inner workings of codeigniter).

Any help will be greatly appreciated!

Thanks,
Max




Theme © iAndrew 2016 - Forum software by © MyBB