Welcome Guest, Not a member yet? Register   Sign In
Form Validation Default
#8

(05-26-2016, 09:49 AM)cartalot Wrote:
(05-22-2016, 05:23 PM)albertleao Wrote: I personally disagree with putting form validations in models. Form validations, as stated, are for forms, which is found in a view which is handled by controllers. Database validation in models is fine, but if you are using your models with different apps without a front end, things can get hairy quickly.

Wouldn't the apps still be running through a Controller?
So here is why i don't like form validation in Controllers - and (again) these are all personal choices no right or wrong implied. If you need to change a form field or add a field in a form - you have to update the View, update the validation rule in the Controller, and update the fields going to the database in the Model. Thats three different places to build out or do changes. If you make a mistake in your view file maybe the form does not render correctly. If you make a mistake in your controller it can bring everything down. So any changes in the controller are more "costly" to your application.

Versus if you put the validation in the model now you just have two places to do changes. In other words a 'thin' controller handles generalities - not details - so it does not have to change very often.

So we have gone from three to two places. Can we refactor to just one place for our build and future changes? Wouldn't that make it much faster and easier to build and update forms? Yes, the next step is preassembling the form elements (using form helper) into an array that goes to the view. Then almost all the changes are constrained to just the model (or library if you prefer). The View at that point becomes "just a view" with absolutely no responsibilities. I'm just starting to experiment with this and at least so far its amazing in how much easier and faster it is to build out large complex forms and their validation.

sure that may work for a small application, but if you work on larger, more complex application where different systems use the same models, the forms might not always be the same.

I've run into this issue several times when developing for companies. It was especially common in Ruby and Ruby on rails. Several companies would have all their models as it's own git repository and have that repo work for multiple applications. At that point, tying a model to a form validation was wrong and we did validation on a per model/db field basis. After learning this in Ruby, I implemented this in my CI apps and have had a ton more flexibility. Now my models might be delivering data to users, services, apis... and I don't have to worry about 'forms'. I can handle all that in the controllers.

In essence, form validation is exactly what it says, form validation which is directly connected to something displayed to the user in a view or an api. A database validation is connected to the database, so that's where you could do field and table constraints on top of the db schema.

Just a different perspective...
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply


Messages In This Thread
Form Validation Default - by edoramedia - 05-22-2016, 12:32 AM
RE: Form Validation Default - by skunkbad - 05-22-2016, 12:40 AM
RE: Form Validation Default - by cartalot - 05-22-2016, 12:06 PM
RE: Form Validation Default - by albertleao - 05-22-2016, 05:23 PM
RE: Form Validation Default - by cartalot - 05-26-2016, 09:49 AM
RE: Form Validation Default - by albertleao - 05-26-2016, 04:29 PM
RE: Form Validation Default - by skunkbad - 05-26-2016, 10:48 AM
RE: Form Validation Default - by mwhitney - 05-26-2016, 02:14 PM
RE: Form Validation Default - by PaulD - 05-26-2016, 09:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB