CodeIgniter Forums
best practices on Form Reuse - keeping all forms in a central location with validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: best practices on Form Reuse - keeping all forms in a central location with validation (/showthread.php?tid=9962)

Pages: 1 2 3 4 5 6


best practices on Form Reuse - keeping all forms in a central location with validation - El Forum - 07-14-2008

[eluser]skattabrain[/eluser]
I'm not sure how to go about this, is there an ideal way to reuse forms? I'd like to reuse a form (take a newsletter signup form), so I can include the form on multiple pages and have all the form and validation logic in 1 place.

I've looked around a bit, but haven't found anything exactly how I want it. I'd like to be able to take any page, pull and validate a certain form, then bring you back on that same page.

Any ideas?


best practices on Form Reuse - keeping all forms in a central location with validation - El Forum - 07-14-2008

[eluser]loathsome[/eluser]
Sure, just create a view (e.g. "common/form.php") and call it wherever you want to use it by using the load function.
Code:
<h2>This is my form</h2>
$this->load->view('common/form');



best practices on Form Reuse - keeping all forms in a central location with validation - El Forum - 07-14-2008

[eluser]skattabrain[/eluser]
thanks loathsome, that i know, my post was poorly written. what i'm trying to reuse is the validation and validate->run aspects.

so i can place this form on any page, but when you post you post back into the same controller, which will then validate the input, and then run the proper code when a validate->run is successful.

does that make sense at all?


best practices on Form Reuse - keeping all forms in a central location with validation - El Forum - 07-14-2008

[eluser]skattabrain[/eluser]
ok, i was making this way to difficult ... placing the validation and form in a view and simply loading it works like a charm ... and my controller is clutter free to boot!

thanks!


best practices on Form Reuse - keeping all forms in a central location with validation - El Forum - 07-14-2008

[eluser]Colin Williams[/eluser]
Quote:placing the validation and form in a view and simply loading it works like a charm ... and my controller is clutter free to boot!

I think the goal is usually the other way around! Controllers are there to serve the burden of complexity. It's the middle-man between Models and Views.


best practices on Form Reuse - keeping all forms in a central location with validation - El Forum - 07-15-2008

[eluser]xwero[/eluser]
Validation shouldn't be in the view but it shouldn't be in the controller either. Validation in the controller makes it unmaintainable pretty fast. Validation is best handled in a separate file but to do that you need to customize the Codeigniter.php file.


best practices on Form Reuse - keeping all forms in a central location with validation - El Forum - 07-15-2008

[eluser]Colin Williams[/eluser]
Quote:Validation shouldn't be in the view but it shouldn't be in the controller either. Validation in the controller makes it unmaintainable pretty fast. Validation is best handled in a separate file but to do that you need to customize the Codeigniter.php file.

Customize the CodeIgniter.php file?... I keep my rules, fields, error messages, and default values in a forms.php config file, but I actually do the Validation running and checking in the appropriate Controller methods.


best practices on Form Reuse - keeping all forms in a central location with validation - El Forum - 07-15-2008

[eluser]xwero[/eluser]
it are not only the things you mention that make the validation take up too much space in the controller but also the control structure, upload manipulations, callbacks, and other things. Keeping things in a config file also makes it easier to loose oversight of what you're checking because you have to mentally make the connection instead of seeing the connection with the validity check.

It's not because the CodeIgniter file is in the system directory it shouldn't be touched. It is a part of the bootstrap file and you should customize it the way you want. By changing the include of the file you can customize it per application but if you want it plain you can use the default one.

A lot of things can be done by customizing the CodeIgniter.php file where you would need to extend the controller instead, which adds another layer to your application. But customizations should span the whole application otherwise extending is a better option.
The OP speaks of forms shown on all pages so that condition is met.


best practices on Form Reuse - keeping all forms in a central location with validation - El Forum - 07-15-2008

[eluser]skattabrain[/eluser]
OP?

I haven't even thought of file uploads and callbacks ... I have the validation in my form view out of frustration, so I definitely would like to see and example of a better way. It doesn't seem correct (although it does work).

For me... my goal is keeping the code DRY without core hacks if possible. At the moment I shy away from core customizations as much as possible.

But xwero, I'd love to see your examples of how you make this work ... this is 1 part of CI that I feel I'm not making the best of ... DRY forms and centralized form validation.


best practices on Form Reuse - keeping all forms in a central location with validation - El Forum - 07-15-2008

[eluser]skattabrain[/eluser]
[quote author="Colin Williams" date="1216112590"]I think the goal is usually the other way around! Controllers are there to serve the burden of complexity. It's the middle-man between Models and Views.[/quote]
I'd agree, it doesn't feel right.