views:

284

answers:

4

Where is the best place to validate data which will be used by model. For example, think about registration form. We have some data which come from registration form. So where is the best place to verify this data.We should check every data by if statements or special validator class, and this means lots of coding, so i want to learn where is the place to do this.

In Controller? or Model?

Both? Because some of the data should be validated by other models?

If you are not sure about exact answer, please try to find possible advantages and disadvantages of both ways.

+1  A: 

Certainly not in the controller, its sole task should be just controlling the request/response and familarize model and view with each other. Do it in the business model. Not with a bunch of if-statements, but just using a for-loop and an abstract validation framework.

Validation in the view should only be done to improve user experience. In webapps the view is basically the HTML page. In that validation is only possible with Javascript which runs entirely at the client side. The client has full control over it, such as hacking/disabling it. Use JS validation only to improve user experience (i.e. quicker response, no flash of content). Still do the (same) validation in the server side for better robustness.

BalusC
But some of the data should be validated by other models?For example, before we add favorite video row , we should check the existence of video .
Oguz
Do it in business model as well. Note, business model, not data model.
BalusC
Sorry , I think i am not really familiar with this terms and their definitions .Can you give any source which i can learn them
Oguz
+6  A: 

The source of the validation data should be in the model while the actual checking should probably be done at both the view level (perhaps with javascript or UI hints) and at the model level. Purists will suggest that the view should not be involved but I disagree.

stimms
The purists are right, but the purists also learned their craft in Desktop application programming. On the web doing it in the view as well as the model has the advantage of saving a round-trip HTTP request. It's easy to pooh-pooh that approach if you have the luxury of a desktop app where everything is on the same machine, often occupying the same memory space. So you're probably right. Do it in both.
RibaldEddie
I wouldn't do validation **only** with Javascript as it can be hacked/spoofed/disabled. Use it only unobtrusively to improve user experience (i.e. no request-response cycles nor flash of content).
BalusC
I agree. The data should be passed to the model (in this case the $_POST array) and be validated there. I also agree on the js in the view.
AntonioCS
A good point by @BalusC. A really nice feature of ASP.net MVC 2 and even version 1 when using xVal is that you can annotate the model with properties like Required and StringLength(50, minLength=10) and it will generate javascript validation as well as validating the model when it is passed into the controller. I don't know how this works in other MVC frameworks
stimms
@stimms.I think it is just the feature of your framework
Oguz
A: 

Putting validation in your models prevents you from having to repeat validation code in a bunch of controllers.

Galen
+1  A: 

validation should be performed in both the model and the view in my opinion.

Paul Creasey