Diego A. Rincon
Diego A. Rincon

Reputation: 768

Where should I perform data verification

I have a form that similar to the "contacts" app. The user fill an form that saves the information in an object called Person.

I want to validate that information and display a message in red right under where there's a mistake. What I don't know is where to validate the data

I was thinking in doing it directly in the view controller when the user press the save button, but I'm not sure it follows MVC principles. I also though in doing it directly in the object class by using setters and return an error message if the data is invalid

Upvotes: 0

Views: 44

Answers (2)

prmottajr
prmottajr

Reputation: 1824

It will depend a little on the technology also. But if we stick to the concept you should think of the purpose of MVC which is to decouple View, Controller and the Model.

If it is really decoupled you could end up having different technologies implementing views to your application but using the same model (depending on how the model is exposed).

So you have to consider what are your constraints, you should consider validating it near the model as a protection to your back-end, but it is an intersting idea to perform some validation on the client (near the view) to improve application performance and decrease network (I assume it is a networked app) usage.

So, use validation near the model (like using a layer for it, or in it) always to improve your code's reusability/autonomy. And use validation on the view to improve application throughput, but keeping in mind that it will have to be implemented on all the views (e.g. android, ios and web) and this could lead to duplication of validation logic, which is bad.

Upvotes: 1

Michael Giba
Michael Giba

Reputation: 118

It would be best if you keep anything that calculates/validates/parses etc. in the model. Think of the view controller as merely a bridge to your model delegating which actions should be performed within it.

Upvotes: 1

Related Questions