Alex Rivera | Logout

MVC - where to implement form validation (server-side)?

Asked 2008-08-25T05:04:30.923
9

In coding a traditional MVC application, what is the best practice for coding server-side form validations? Does the code belong in the controller, or the model layer? And why?

Edit
Report

2 Answers

4

From Wikipedia:

Model-view-controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other. In MVC, the model represents the information (the data) of the application and the business rules used to manipulate the data; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages details involving the communication to the model of user actions such as keystrokes and mouse movements.

Thus, model - it holds the application and the business rules.

answered 2008-08-25T05:09:41.800
4

I completely agree with Josh. However you may create a kind of validation layer between Controller and Model so that most of syntactical validations can be carried out on data before it reaches to model.

For example,

The validation layer would validate the date format, amount format, mandatory fields, etc...

So that model would purely concentrate on business validations like x amount should be greater than y amount.

answered 2008-08-25T05:27:11.883

Your Answer