Handling input validation

From a design perspective, I am using a controller which gets passed a form submission for a new user add. I have a user class that handles adding, changing, etc. From a best practices standpoint, where is it best to handle data validation tasks (assuming no javascript for the moment) such as the field is filled in, it is a valid username and email, the password fields match, valid zip code, etc., etc.

It seems to me (which is always a warning sign for me :D) that validations specific to a user should go into my user class with a different method for each validation type and validations that may be used across classes such as email validatiion would then go into a helpers file and be included by reference into the class. I would then call a controlling method in the user class for the add which progresses through the necessary validations and the add process and exits if there are problems sending back an error code and message.

Any comments on this approach would be very welcome. Thanks

Don’t try to make a class do too much – IMO a user object should not be responsible for validating itself; that is a separate responsibility for another class/object.

I would create a separate UserValidator class with the User object passed in the constructor, and from there on I think your idea is good. Also, a UserValidator class sounds like a good candidate to be extended from a base ‘ObjectValidator’ class :). HTH

Makes perfect sense. Let me try that. Thanks again.