• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form Validation callbacks not passing returned value to next rule

#1
[eluser]september28[/eluser]
Hi There,

i am running CI reactor v 2.0.1 and have created some callback rules for my form validation. They are basically decoding a field that has been encrypted using Codeigniter's encrypt library. In this case I am Encrypting an email address (the string) and salting it with the API Key that the controller is being passed (the controller is accessed through an api call and X-API-KEY header is set to the correct API key).
Here is my rule code:
Code:
$email_params = "{$this->input->post('email')},{$this->rest->key}";
    $em_t = $this->encrypt->decode($this->input->post('email'),$this->rest->key);
    log_message('debug',$email_params);
    log_message('debug',$em_t);
    log_message('debug',$this->_decode_hash_rule('blah',$email_params));
    $this->form_validation->set_rules('email','Email',"required|callback__decode_hash_rule[$email_params]|valid_email");
As you can see, my callback rule is called _decode_hash_rule and takes both the raw post string and the $email_params variable.
The log_message calls are in for debugging purposes and all display the correct values in the log (this first shows the encrypted string and key, the second shows the decoded email and the third shows the decrypted email as returned by the callback function itself).

My callback function is the following:
Code:
/*
     * _decode_hash_rule
     * This is the form validation rule version of the _decode_hash method
     */
    function _decode_hash_rule($str='',$params='')
    {
    list($email_hash, $key) = explode(',', $params);
    if( ! $ret = $this->_decode_hash($email_hash, $key))
    {
        $this->form_validation->set_message('_decode_hash', lang('api_proper_encryption'));
        return FALSE;
    }
    log_message('debug','decode_hash_rule returns: '.$ret);
    return $ret;
    }
    /*
     * _decode_hash(str, params)
     * This is a form validation function that decodes the hashed email.
     * If the hashed string is correctly decoded, it is passed back as an unhashed
     * string, ready for any other validation rules (such as valid_email)#
     * if it fails, an error message is set, and the nothing is returned (string
     * will remained unchanged).
     */
    function _decode_hash($str='', $key='')
    {
        $decoded = $this->encrypt->decode($str, $key);
        if($decoded)
        {
            return $decoded;
        }
    else
    {
        return FALSE;
    }
    }

As you can see I also have a debug log message on the _decode_hash_rule method which also returns the decoded email address ([email protected] - the "Xs" are just there to blur the email)

i also have put a debug log on the valid_email method in the Form_Validation library (system/libraries/Form_Validation.php). This returns the ENCRYPTED string still which prooves that the cascading rules are not functioning properly. Since the encrypted string is not a valid email, I receive the valid email error and so the form doesnt pass validation.

In the Codeigniter userguide it states that the rules will be run right to left and either return booleen true or false, or return the processed value. if the processed value is returned, it will be used in all subsequent rules. In my example it doesn't seem to be happening. Does anyone know why this might be happening? Is this a bug in Codeigniter?

Cheers,

Dan


Messages In This Thread
Form Validation callbacks not passing returned value to next rule - by El Forum - 05-23-2011, 05:26 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.