Welcome Guest, Not a member yet? Register   Sign In
Validation callbacks in libraries.
#1

[eluser]Chris Newton[/eluser]
I have a registration library which works great, except for custom callbacks. They just get totally ignored.

$rules['username'] = 'callback_username_check';

This callback, when added to a library is completely ignored, unless that function exists in the calling controller. Is there some sort of method to use callbacks within a library? Maybe using a helper in addition to the library? I'm going to try that...
#2

[eluser]Chris Newton[/eluser]
Nope. that doesn't work. Well, at least I can't figure out how anyway.
#3

[eluser]Nanodeath[/eluser]
In libraries/Validation.php, I found this relevant bit of code:

Code:
if ($callback === TRUE)

                {

                    if ( ! method_exists($this->CI, $rule))

                    {        

                        continue;

                    }

                    

                    $result = $this->CI->$rule($_POST[$field], $param);    

                    

                    // If the field isn't required and we just processed a callback we'll move on...

                    if ( ! in_array('required', $ex, TRUE) AND $result !== FALSE)

                    {

                        continue 2;

                    }

                    

                }

                else

                {                

                    if ( ! method_exists($this, $rule))

                    {

                        /*

                         * Run the native PHP function if called for

                         *

                         * If our own wrapper function doesn't exist we see

                         * if a native PHP function does. Users can use

                         * any native PHP function call that has one param.

                         */

                        if (function_exists($rule))

                        {

                            $_POST[$field] = $rule($_POST[$field]);

                            $this->$field = $_POST[$field];

                        }

                                            

                        continue;

                    }

                    

                    $result = $this->$rule($_POST[$field], $param);

                }

Now, make of that how you will...I don't have the solution, but one hack you could try would be to not prefix your define with callback_, and then define it as a function just below your library instead of a method in your library. Obviously not a great solution, but I'm not sure if there really is one, unless you want to modify the Validation library to iterate through every loaded library to try to find a corresponding method.
#4

[eluser]Chris Newton[/eluser]
I ended up using regular validation operations.

THEN I added private functions at the bottom of my library. These private functions are called at the end of the validation testing like so:

Code:
$this->_username_check($CI->validation->username);

If those private functions fail, they do something like this:

Code:
$CI->validation->username_error='<span class="error">That username is taken</span>';
#5

[eluser]wiredesignz[/eluser]
I posted a week ago, this change to the validation library allows callbacks into models or libraries.

http://ellislab.com/forums/viewthread/69797/
#6

[eluser]tomcode[/eluser]
I extend the Validation Class with my callbacks.

Code:
class MY_Validation extends CI_Validation{

  function callback($str)
  {
     //...
  }

}




Theme © iAndrew 2016 - Forum software by © MyBB