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

[eluser]skattabrain[/eluser]
sorry to keep posting, but i still am not sure how to centralize form validation. any ideas?

can you keep validation logic in a library?

i can create a new controller and call it 'form_processor' then use a view to load the form on any page i desire and post into 'form_processor', but when the validation rules detect a field isn't correct ... i will no longer be on the same form.

argh! i'm so confused with this now. it's like i need to simply pull the validation with a good ole php include. (which by the way seems to work well ... although i haven't tried a callback yet)
#12

[eluser]chuckleberry13[/eluser]
I'm having the same problem. I've looked into maybe using view partials for this, but still caught up on how to best include forms with their respective validation rules in one central location.
#13

[eluser]chuckleberry13[/eluser]
What I ended up doing is using Modular Extensions and just loading making the form into a view partial. It seems to be working ok for now.
#14

[eluser]Colin Williams[/eluser]
Quote:still caught up on how to best include forms with their respective validation rules in one central location

The "one central location" is a Controller method! The one that should handle the form submission. Even though your newsletter form, for example, displays on every page, it should still always submit to the same Controller (if you're following the MVC pattern). Now, this doesn't necessarily mean that you have to end up on the same page every time, just submit to the same page (there are ways to redirect).

Have y'all checked out the user guides coverage of the Validation library. It takes you through the entire process of running validation from a controller, and using validation properties in your view.
#15

[eluser]chuckleberry13[/eluser]
@Colin - that sound great in theory and maybe you could enlighten us on how to overcome the following problem. To use a form with the validation library you have to set the rules and fields before you display the form and every time you display the form if you plan on re-populating the form fields (which I do).

I have a form that works great in one controller because that controller contains all the validation rules and fields, but how do I include that form in another controller without having to reset the validation rules/fields?
#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
#17

[eluser]Colin Williams[/eluser]
@chuckleberry13

I think you're making some false assumptions (for lack of a better term).

Quote:To use a form with the validation library you have to set the rules and fields before you display the form and every time you display the form if you plan on re-populating the form fields (which I do).

Not true. You don't have to. Sure, the burden increases in your form view code because you must first check for the existence of Validation properties [ if (isset($this->validation->name)) {...} ], but this certainly doesn't amount to a requirement.

(Wait a second, that brings up another point. Although the user guide demonstrates otherwise, I suggest against accessing Validation class properties from your view files [or any CI class properties for that matter]. You should use a standardized method of ALWAYS passing form values, empty or otherwise, to your views.)

Quote:how do I include that form in another controller without having to reset the validation rules/fields?

First ask yourself this: Why the heck do I have two controllers processing the same form? Maybe that's not what you meant. If not, please clarify.

@randy Casburn

That's straight-up crazy nonsense. Check yo'self, fool.
#18

[eluser]chuckleberry13[/eluser]
@randy Sorry if this is a newbie question but what is the form object?
#19

[eluser]Colin Williams[/eluser]
Pay no attention to Randy. He's clearly gone off his rocker.
#20

[eluser]Randy Casburn[/eluser]
[quote author="chuckleberry13" date="1216972185"]@randy Sorry if this is a newbie question but what is the form object?[/quote]

Sorry -- bad use of the term object, should have said form data. Get caught up in too many technologies at once.

@Colin -- The only thing that has been done is simply taking the validation stuff out of the controller and placing it into a externally callable resource that is managed by the controller.

Since it is no longer "in a controller", these validation rules can be generalized to meet a broader scope.

I am dead serious...it is true that I went insane some time ago though.

think about this..you'll see the possibilities here.

Randy




Theme © iAndrew 2016 - Forum software by © MyBB