CodeIgniter Forums
Form Validation on extended MY_Model - Printable Version

+- CodeIgniter Forums (
+-- Forum: Development (
+--- Forum: CodeIgniter 3.x (
+--- Thread: Form Validation on extended MY_Model (/showthread.php?tid=65282)

Form Validation on extended MY_Model - edoramedia - 05-24-2016

I know that everyone mentions form validation needs to be done on the "model" side.
However I am having trouble wrapping my head around best practice for the following scenario:

I have a MY_Model that has the basic CRUD setup.

Let's say I now have a "user" table where I extend the MY_Model

In "insert" if I want to "insert" through backend I may need to put a lot more items (more "required" items); however if someone "insert"s their details in the register page of the website the form validations may completely be different. I may have "required" fields in backend that are not "required" in registration.

How do I handle something like this dynamically?

RE: Form Validation on extended MY_Model - cartalot - 05-24-2016

there is nothing wrong with having more then one form validation method in different places if thats the clearest way to do it. if you have one form for a backend admin, and a very different form for a front end user - keep them separate if thats what makes the most sense for your application flow. saving code should never be at the expense of making your application more difficult to maintain. otherwise if you have one set of form fields and validation that they both share - then you could call that one for both, and call a separate validation method that has the extra fields. there is no problem with calling two validation methods it will work just the same.

one of the ideas with having the form validation in a model - is then you can create the array that goes to your database method in the same place. then you have easy access to the form field names that were used. and when/if form fields get added or changed, you can just go to the model to make those changes, instead of also having to do it in a controller.