Welcome Guest, Not a member yet? Register   Sign In
Form validation callback functions file
#1

[eluser]Mike Chojnacki[/eluser]
Hi, I'm trying to figure out a way to have a file with callback functions for the form validation callback rules. Having to copy paste and hard-code the ones you need into every controller is rather un-intuitive.

I have tried to use it as a helper and as a library, ultimately the problem boils down to the function not having access to the form_validation object and no way to pass it along when calling the callback_ functions in the form rules.

config/form_validation.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
                'login' => array(
                                array (
                                    'field' => 'username',
                                    'label' => 'Username',
                                    'rules' => 'required'                                    
                                ),
                                array (
                                    'field' => 'password',
                                    'label' => 'Password',
                                    'rules' => 'callback_password_check'
                                )    
                )
);

application/library
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class validation_callbacks {

    function password_check($str)
    {
        $strlen = strlen($str);
      
        if($strlen == 0)
        {
            return true;
        }
        elseif($strlen > 0 && $strlen < 8)
        {
            $this->form_validation->set_message('password_check', 'The password has to be at least 8 characters in length.');
            return false;
        }
        
        return false;  
    }
    
}

application/helper
Code:
function password_check($str)
    {
        $strlen = strlen($str);
      
        if($strlen == 0)
        {
            return true;
        }
        elseif($strlen > 0 && $strlen < 8)
        {
            $this->form_validation->set_message('password_check', 'The password has to be at least 8 characters in length.');
            return false;
        }
        
        return false;  
    }

function_exists('password_check) returns true, but the callback isn't initialized either way. Has anyone found a workaround for this problem? I refuse to believe that having the same function hard-coded in every controller that needs it.
#2

[eluser]InsiteFX[/eluser]
Might work in a MY_Controller, but I have not tried it.
Give it a try and see if it will work.

InsiteFX
#3

[eluser]Mike Chojnacki[/eluser]
[quote author="InsiteFX" date="1299877744"]Might work in a MY_Controller, but I have not tried it.
Give it a try and see if it will work.

InsiteFX[/quote]

Hi InsiteFX, thanks for your advice. It did try it, but it did not work straight away.
Here is the "fix" I applied.


applications/libraries/MY_Form_validation.php
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {
    
    function password_check($str)
    {
        var_dump($str);
        $strlen = strlen($str);
      
        if($strlen == 0)
        {
            return true;
        }
        elseif($strlen > 0 && $strlen < 8)
        {
            $this->set_message('password_check', 'The password has to be at least 8 characters in length.');
            return false;
        }
        return true;  
    }
    
}

system/libraries/form_validation.php [start at line 589]
Code:
// MODIFIED
                if ( ! method_exists($this->CI, $rule) && ! method_exists($this, $rule))
                {
                    
                    continue;
                }

                // Run the function and grab the result
                if(method_exists($this, $rule))
                {
                    $result = $this->$rule($postdata, $param);
                }
                elseif(method_exists($this->CI, $rule))
                {
                    $result = $this->CI->$rule($postdata, $param);    
                }
// END MODIFIED




// ORIGINAL
                if ( ! method_exists($this->CI, $rule))
                {
                    
                    continue;
                }

                // Run the function and grab the result
                $result = $this->CI->$rule($postdata, $param);
// END ORIGINAL

Now, you can just add functions like "password_check" to MY_Form_validation and call them wherever you want, if no function is found it will fall back and check in the controller if that callback validation function exists there.

I just did it quickly now, does anyone have any input on as how to accomplish this effect without having to directly edit a system library file?

I really like this way much better cos I can have all my callbacks in one file and not clutter my controllers with them and reproduce code over several pages.
#4

[eluser]junkwax[/eluser]
Exactly what I needed, thx.

I will use that method until something similar is natively included in CI




Theme © iAndrew 2016 - Forum software by © MyBB