CodeIgniter Forums

Full Version: Use Validation Rules without loading a page from a controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Brian Bommarito[/eluser]
Okay, my question is this: Is it possible to attach rules to a form if it's not loaded from a controller?

I want to have a quicklogin on all pages of the site. Basically, it will probably be loaded from within the controller function to view the page. Is it possible to setup one set of rules for that form in another controller, and use those to validate that form from, or would I have to setup rules in every single controller function that it's called from?

El Forum

[eluser]Phil Sturgeon[/eluser]
Correct me if im wrong, but as long as the code is being called in the same page request as $_POST is being set (As far as I know).

El Forum

[eluser]Glen Swinfield[/eluser]
Just make a controller called QuickLogin() or perhaps "Test()" or something and just have the form post to that - validation will be done and then redirect('to_page_wharever') to wherever you need to after validation/login is complete. It doesn't matter where the login is done.

You definatley don't want to include the functionality in every controller - which I think is what you are suggesting? MVC architecture exists to prevent that sort of repetition, so if it looks like you are going to have to keep repeating code - there is probably a better way.

El Forum

[eluser]Phil Sturgeon[/eluser]
Agreed, I answered the question in the title without reading all of the post. You want to make a controller that the form can be sent to, no matter what page the form is being shown on.

Example, have a controller called account.php then have a method called login() which will have the validation in. Set the form action to /account/login and bingo.

You only have the validation in one page and you have the effect you wanted.

El Forum

[eluser]esra[/eluser]
It sounds like he wants to include a login block (i.e., view fragment or partial) in a main view or template, regardless of which module is loaded.

El Forum

[eluser]Brian Bommarito[/eluser]
esra has it right.

I want to include a block of code to do the quick login on a number of pages. Now, looking at validation, I don't know if it would work without having the logic in all the views it's in because you have to perform the run() on it, and without knowing exactly where it is (IE what page it's on) I can't code that properly.

El Forum

[eluser]Glen Swinfield[/eluser]
I'm still not sure what it is you are trying to do. Basically, if you want a login page in many views I would do it like this:

Create a view fragment containing the html for the form, include this in all of the views that you wish to have the form in.

Have a function in a Model that returns an array of fields and rules for validation (you could put this in a controller, I put it in a Model).

In a controller have the action to perform the validation when the login details are posted, this action will then redirect the logged in, or failed logged in user back to the page they were on.

El Forum

[eluser]Brian Bommarito[/eluser]
Okay, I will give that a shot then.

That sounds like the best solution, thank you for the advice.

El Forum

[eluser]Brian Bommarito[/eluser]
I must be fairly dense, because I cannot get this to work at all.

Codepat, you willing to post a little sample code so I can get the idea? Because I must not be doing this properly, or thinking about it all wrong.

El Forum

[eluser]Glen Swinfield[/eluser]
I can - I'll get back to you as soon as possible.
Pages: 1 2