Welcome Guest, Not a member yet? Register   Sign In
Central form handeling
#1

[eluser]GrahamDj28[/eluser]
Hi,

I have been using CI for a few years now, and there is one thing that i do not like. That is the way forms are handled.

Does anybody know of a mechanism to centralize the handling of posted forms so that i don't need to validate in every controller that uses a form? The URL used in the action attribute of the form can then be used to redirect after success or failure. That does mean that the posted forms should always be routed to a single place where all the magic happens.

The routing is not a big deal. The problem is setting the rules for the form. I don't want to define my form in one place and define my rules for that form in another.

Anybody here knows how this can be done?

Thanks!
#2

[eluser]CroNiX[/eluser]
How is CI supposed to override default web behavior by not submitting the form to the url in the Action attribute?

If you really want, you can define all of your validation in a custom library and then just load that library and call the specific method to run validation for that controller.

Code:
if ($this->input->post('submit'))
{
  //if the form was submitted

  $this->load->library('forms');
  $passed = $this->forms->validate->some_method_for_this_controller();
  //where some_method_for_this_controller is returning the result of running form_validation->run()
}

Your forms library would load the validation_library in the constructor, and have methods for each form with the rules defined in each method.
#3

[eluser]PhilTem[/eluser]
Technically speaking, the OP might want to create a controller called forms and have each form's action point to
www.example.com/forms/example_form
where a _remap function would catch every call and perform the form-validation based on a hidden input field.
However, any form-validation errors will be lost since they are flash-messages and you will have a redirect after the form was submitted (technically you should want to have a redirect afterwards) thus you have to work around the issue of loosing your form-validation errors.

Furthermore, you can have a global configuration of form-validation options in APPPATH . 'config/form_validation.php' which will be auto-loaded if you load the form-validation library. Read into the user's guide on that topic
#4

[eluser]GrahamDj28[/eluser]
[quote author="CroNiX" date="1351012590"]How is CI supposed to override default web behavior by not submitting the form to the URL in the Action attribute?
[/quote]

No need to override default web behavior. Every request is routed through the index.php and the URI class is always called. In my CI setup I only have 1 front end controller and 1 back end controller. I have extended the URI class to filter out extra params that can be set in the URL. You could say all my URLs are virtual and they all get routed through 1 of the controllers depending on the call.

That being said, just because the form has an action URL does not mean that is has to be processed by that "page". A check can be done in the controller where the request is handled to see if a form has been posted.

I figured this out after I posted the question and so I have centralized the handling of forms. I also got fed up with writing controllers for every page, and have setup a kind of HMVC where modules do the real stuff and return partial views. I like to keep files that have a relationship together in a single package so moving them to a new project is no big deal. To have the request execute the proper module and methods the _remap functions is used as PhilTem mentioned in his reply.

And the redirecting is only done on success and on failure the re-populated form with errors is returned.
#5

[eluser]PhilTem[/eluser]
What just came to my mind: You could also put the magic into your frontend and backend controllers __construct()-method. Just check for some specific $_POST-flag and then execute the form-validation. This way you don't need a separate controller for handling forms and you won't loose the flash-messages of the form-validation library.

Wink
#6

[eluser]GrahamDj28[/eluser]
That's how it is being handled now. But it is a call to the real magic. I have created a formbuilder library and that has some functions that call the validation and do the re-populating on failure.

Thanks for your thoughts!




Theme © iAndrew 2016 - Forum software by © MyBB