Welcome Guest, Not a member yet? Register   Sign In
best practices on Form Reuse - keeping all forms in a central location with validation
#16

[eluser]Randy Casburn[/eluser]
@skattabrain & @chuckleberry13 -- you two have some of the coolest handles ever! Man I wish I had an imagination...ok..done hijacking.

@skattabrain -- Now...I am liable to get flamed, so I have my flame retardant suit on. Why? Because there are some folks here that just want to run rampant on producing world class Super Structure Aircraft Carrier/Battleship/Destroyer USS Enterprise Object Oriented behemoths that are so complicated and intertwined and upside down and inside out on the magnitude of the Milkyway in order to spell your name with a PHP script that is going to run for approximately .09618 seconds...like this paragraph...right?

So here comes the flame worthy post...it's simple.

Controller... welcome.php
Code:
class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
    }
    
    function index()
    {
        $this->load->view('welcome_message');
    }

        function login()
        {
            $this->load->plugin('stdLoginFormValidator');
        
            if (stdLoginFormValidator($this) == FALSE)
            {
                $this->load->view('login');
            }
            else
            {
                $this->load->view('login_success');
            }
        }
}
<?php ?>
View...login.php
Code:
<html>
<head>
<title>My Form</title>
</head>
<body>

<?php echo $this->validation->error_string; ?>

<?php echo form_open('welcome/login'); ?>

<h5>Username</h5>
&lt;input type="text" name="username" value="" size="50" /&gt;

<h5>Password</h5>
&lt;input type="text" name="password" value="" size="50" /&gt;

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="" size="50" /&gt;

<h5>Email Address</h5>
&lt;input type="text" name="email" value="" size="50" /&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;


CI Plugin...stdLoginFormValidator_pi.php
Code:
&lt;?php
function stdLogInFormValidator(& $obj ){
    
    $obj->load->helper(array('form', 'url'));
    
    $obj->load->library('validation');
    $rules['username']    = "required";
    $rules['password']    = "required";
    $rules['passconf']    = "required";
    $rules['email']        = "required";

    $obj->validation->set_rules($rules);

    return $obj->validation->run();
}
?&gt;

Using a standard CI plug in for each "collection of form entities" you can maintain collections of form rules.

Now, extend your thinking a little. Notice I've passed in the CI super object by reference. That means you have access to the form object. You could start to consolidate groupings of validation rules without knowing what's on the form. Since you have access to the form object, you could actually test for the HTML element prior to setting any rules. Then only run the rules for active elements.

Next, someone will bring up callbacks. You'll need to decide where you want the call back functions to reside. If your design would have you put your callback with the validation rules (makes sense to me), then you may consider making this whole thing a "helper" rather than a "plugin". They have this hang up about "plugins" being single function thingys. Hey, if it were me, and this did the trick. I wouldn't worry about it.

What I've provided you is a flattly simple way of consolidating your Validation rule sets and callback functionality based upon "collections of form field sets".

Hope this helps.

Randy


Messages In This Thread
best practices on Form Reuse - keeping all forms in a central location with validation - by El Forum - 07-24-2008, 07:58 PM



Theme © iAndrew 2016 - Forum software by © MyBB