Welcome Guest, Not a member yet? Register   Sign In
Model and Form
#12

(12-12-2014, 04:55 PM)bclinton Wrote: Perhaps it's a matter of personal preference, but I don't agree with that method.  Form Validation is a natural function of the controller in my opinion.  It should be the first thing your controller does in most cases.  The controller is opening the door to your server when it accepts requests and form input.   The lock should be on the front door, not in the kitchen.

Forms pass data to a controller.   When that form data does not validate, the controller sends the data back to the user with error messages.  Why should the model enter into this clear request/response exchange when it is completely unnecessary?  Why should you even load a model if you know it's not going to be needed in the case of failed validation?

If you have multiple controllers accessing the same model, you end up repeating validation which is required by the model. If you change the model or database in a way that changes the validation, you then have to change that validation in multiple controllers.

Additionally, the controller shouldn't need to know anything about the database fields used by the model, but the model will have to ensure that input is validated according to the database requirements. If you don't use form validation to validate the database requirements of the model, you will still have to do something which happens to be equivalent to form validation.

This is why I specifically allow for both controller-specific validation and model-specific validation by storing validation rules in the model, but running them from the controller. The controller may have reason to add other validation rules, or even use validation rules from multiple models, but the model is really the only place that needs to know details like the type and maximum length of a given field in the database.

Any additional overhead from loading a model in a controller which handles methods to save data has not been an issue for me so far.
Reply


Messages In This Thread
Model and Form - by aurelien - 11-20-2014, 03:24 AM
RE: Model and Form - by Rufnex - 11-20-2014, 04:10 AM
RE: Model and Form - by ivantcholakov - 11-20-2014, 05:00 AM
RE: Model and Form - by bclinton - 11-20-2014, 07:59 AM
RE: Model and Form - by slax0r - 11-20-2014, 04:11 PM
RE: Model and Form - by RobertSF - 11-20-2014, 05:59 PM
RE: Model and Form - by bclinton - 11-20-2014, 07:26 PM
RE: Model and Form - by slax0r - 11-21-2014, 01:43 AM
RE: Model and Form - by bclinton - 12-12-2014, 04:58 PM
RE: Model and Form - by mwhitney - 12-12-2014, 03:21 PM
RE: Model and Form - by bclinton - 12-12-2014, 04:55 PM
RE: Model and Form - by mwhitney - 12-15-2014, 10:02 AM
RE: Model and Form - by jaynarayan - 12-18-2015, 10:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB