Design decisions for validation

I am looking for the right place to validate the input in a textfield basically in Java. This is specific for a MVC based construct. The main question to be answered is: is the validation part of the model or the view. Let’s have a look at the view.

What are the advantages if the validation is in the view? What are the disadvantages?
One of the advantages is the relative easy and quick access to the UI component, e.g. a JTextfield. With the according listener it is possible to react on each character entered into the field. This would led to a fast solution. The big caveat would be to violate the principle of the MVC pattern. We would have business logic in the view component. When I think more about this behavior we would even be against the single responsibility principle. A class should have only one reason to exist.
One further caveat will be to enter the validation code for every component, I think here of forms, in this view as a inner class. This would led to a violation of the DRY principle. DRY stands for Don’t repeat yourself. One of the advantages of this is that the view don’t has to communicate with the according model over a server or something like an internal messaging system this would led to a simpler implementation of the system.

What would happen when the validation occurs in the model?
Advantages of this is a clear separation of the behavior. It would be possible to reuse all the validation code for various view parts. What is the price to get to this point? There would be an overhead to get to this point, e.g. we need some kind of a message system to use to communicate between the view and the model. This will be needed in every case because of the nature of this kind of pattern. If the system is huge we can run in some timing problem when the server connection isn’t fast. But in a local system it should be an appropriate way to communicate. In my opinion and with my knowledge I would consider the right place for the validation somewhere in a model component.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s