OK i have seen other posts about this but none really specifically answer my question.
Where in an application should validation logic be?
I have a small application that allows new products to be instered into an applications database. There are different products with different fields i.e. product name, order number, description etc. New products can be inserted and existing products can be updated. Therefore when a new product is being inserted then all fields must be validated but when an existing product is being updated then only the fields being updated need to be validated i.e. maybe just the descirption is being updated so only that field should be validated.
Im thinking of an abstract class and two concrete classes for full and part product validators, each having their own validation logic contained at class level.
I have the feeling though that there must be a better pattern for this – any advice?
Depends on your architecture. Validation can be handled in multiple stages to achieve responsiveness. Typically, though the model/controller seems like a good place in a given MVC architecture. This question came up sometime back on Joel’s old forum in context of the MVC architecture. It seems plausible that the model should be responsible for accepting/rejecting input.
Yes.
You cannot possibly foretell which exact part will be updated. So, you’ll need to write validators for all fields (columns of your database).
You could simplify life and have a single validator class (unless of course validating a particular set of attributes is too complicated/time consuming).