Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put validation logic in MVC software architecture

I am actually starting to learn the mvc architecture.

I'm confused on whether to place my username registration validation logic in model or in controller.

I have some sort of status message that would tell the user whether the new username to register is available or not.

My confusion started because most sources say that it should be in the model because it involves the username data to validate before placing it on the database (rather than checking inputs to the username field). However, the status message should respond immediately prior to the change in the username field by user keypress or change, which lead me to think that it should be in the controller because it involves more on user events.

My concern is not actually on the framework to use but on the standard concept involving MVC. Where do I put the username validation logic based on the conditions/premise above?

like image 803
Neigyl R. Noval Avatar asked Nov 11 '11 17:11

Neigyl R. Noval


People also ask

Where does validation go in MVC?

Validation should be in the domain layer, but it can also be used in other layers, like the UI (probably in the Controller or the View in Js) to give fast feedback to the user.

Is validation part of business logic?

Business logic tier validation assumes existing data is valid. The business logic tier employs validation logic when it needs to create or change data, but it assumes that data already in the table is valid.

Should validation be done in controller?

If you're validating data on server side, And your validation does not require application business logic (i.e you're not checking to see if the user has enough credit in his account), You should validate in the controller.

Which methods can be used to perform validation in MVC?

The following three type of validations we can do in ASP.NET MVC web applications: HTML validation / JavaScript validation. ASP.NET MVC Model validation.


2 Answers

As Shikhar says, the actual checking of whether the name is acceptable/available is a Model responsibility. The controller can provide an action that is called by some AJAX on the page, so that as each key is pressed, the text on the page is sent to the dedicated controller action which then validates it through the model (anything that touches the database is Model).

There are a couple of things to consider in the view, such as when the user is typing quickly, you should cancel the previous calls before making the new one as this can get confusing.

Also the controller post action that happens when the user submits the form at the end of their data entry should perform the same validation as the AJAX action did just to avoid race conditions between users.

like image 78
Colin Desmond Avatar answered Sep 21 '22 06:09

Colin Desmond


It should be in the model as you have read yourself. I think you are getting confused between the "validation process" and "validation rules". Validation process will either be in controller on the client side but validation rules are properties of model.

like image 44
shikhar Avatar answered Sep 23 '22 06:09

shikhar