<p>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?</p>

[{'answer_id': 25678, 'author': 'Josh', 'author_id': 257, 'author_profile': 'https://Stackoverflow.com/users/257', 'pm_score': 3, 'selected': True, 'text': '<p>From Wikipedia:</p>\n\n<blockquote>\n  <p><a href="http://en.wikipedia.org/wiki/Model_View_Controller" rel="nofollow noreferrer">Model-view-controller</a> (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.</p>\n</blockquote>\n\n<p>Thus, model - it holds the application and the business rules.</p>\n'}, {'answer_id': 25692, 'author': 'jatanp', 'author_id': 959, 'author_profile': 'https://Stackoverflow.com/users/959', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>For example,</p>\n\n<p>The validation layer would validate the date format, amount format, mandatory fields, etc... </p>\n\n<p>So that model would purely concentrate on business validations like x amount should be greater than y amount.</p>\n'}, {'answer_id': 25721, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 0, 'selected': False, 'text': '<p>My experience with MVC thus far consists of entirely rails.</p>\n\n<p>Rails does it\'s validation 100% in the Model.<br>\nFor the most part this works very well. I\'d say 9 out of 10 times it\'s all you need.</p>\n\n<p>There are some areas however where what you\'re submitting from a form doesn\'t match up with your model properly. There may be some additional filtering/rearranging or so on.</p>\n\n<p>The best way to solve these situations I\'ve found is to create faux-model objects, which basically act like Model objects but map 1-to-1 with the form data. These faux-model objects don\'t actually save anything, they\'re just a bucket for the data with validations attached.<br>\nAn example of such a thing (in rails) is <a href="http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects" rel="nofollow noreferrer">ActiveForm</a></p>\n\n<p>Once the data gets into those (and is valid) it\'s usually a pretty simple step to transfer it directly across to your actual models.</p>\n'}, {'answer_id': 42569, 'author': 'Czimi', 'author_id': 3906, 'author_profile': 'https://Stackoverflow.com/users/3906', 'pm_score': 0, 'selected': False, 'text': '<p>The basic syntax check should be in the control as it translates the user input for the model. The model needs to do the real data validation.</p>\n'}]
['https://Stackoverflow.com/questions/25675', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']