Welcome Guest, Not a member yet? Register   Sign In
Auto Load Form Validation Library based on existance of post array.
#1

[eluser]Henry Weismann[/eluser]
I was thinking of creating a hook right after the controller is called to load the form validation library and attach it to the controller if the POST array isset.

I was thinking this is the best way to reduce a line of code in each controller and also to not have to load it when it is not needed. The only downfall I can think of is that now im loading a hook on every page load instead but maybe loading a hook is less intensive then always auto loading the form validation.

Im thinking it would be like this...

Code:
function load_form_validation()
{
$CI = & get_instance();

if(isset($_POST))
{
$CI->load->library('form_validation');

}

}

Now Im not sure if that would make it accessible by the controller? Also does anyone see any reasons this is a bad idea?
#2

[eluser]davidbehler[/eluser]
Isn't $_POST always set but it's either empty or not?
#3

[eluser]Henry Weismann[/eluser]
[quote author="waldmeister" date="1248459168"]Isn't $_POST always set but it's either empty or not?[/quote]

Well I have always found that it isn't set. This is why something like this will throw an error:

Code:
if($_POST)
{
//do somthing
}

and we have to use

Code:
if(isset($_POST))
{
//do something
}
Or maybe Im thinking of when I get an actual key $_POST['submit']...either way I can atleast count($_POST) probably.
#4

[eluser]Phil Sturgeon[/eluser]
waldmeister is right, $_POST should always be set. Either way, just use if(empty($_POST)) which covers !isset(), FALSE, 0, array(), etc.
#5

[eluser]Henry Weismann[/eluser]
Yes, it looks like he is...i guess i was thinking of when I tried to get an actual key. I use the form validation library exclusively so it has been a while since I used $_POST directly.

Anyway...any input on the hook. I'm pretty sure that would attach the library to the controller.
#6

[eluser]davidbehler[/eluser]
On the hook:
I guess that should work. Sounds like an easy way to save one line of code for each function in the controller that deals with validation. Should have thought of it myself Big Grin

Although there might be cased where $_POST is not empty but you don't need validation either...
#7

[eluser]Henry Weismann[/eluser]
Well, I figured doing is better then talking so I gave my first hook a shot...

hooks/Preload.php

Code:
class Preload
{

        function Preload()
        {
        
        }
        
        function load_form_validation()
        {
        
            if(!empty($_POST))
            {
                $CI = & get_instance();
                $CI->load->library('form_validation');
            }
        
        }
}

In config/hoooks.php

Code:
$hook['post_controller_constructor'] = array(
                                'class'    => 'Preload',
                                'function' => 'load_form_validation',
                                'filename' => 'Preload.php',
                                'filepath' => 'hooks'
                                
                                );

In config/config.php

Code:
$config['enable_hooks'] = TRUE;

It preloads the library quite nicely. I also created an url validation library so I can load that when $_GET is !empty. Can anyone else thing of any useful hooks. I'm going to take a look at the wiki for any other useful hooks. I have never seen any hooks around the forum so I'm curious if anyone is using them and how.
#8

[eluser]Henry Weismann[/eluser]
I just realized in order to set the rules and use the run function before the post has happened the lib needs to be loaded. O well.




Theme © iAndrew 2016 - Forum software by © MyBB