Sunday, May 04, 2008

I've been putting some thought into validation in the context of an MVC Framework application. Recently I wrote about using Extension Method Validators as a neat way of expressing validation rules in your domain entities. These validators throw a ValidationException when they trigger.

Now in a user interface we would really like to see all the validation failures for a form. So we need to collect the exceptions as they occur and then continue processing. Here's a neat little Validator class that's simply a collection of Action delegates that executes them when its Validate method is called:

Since the 'property' variable is an empty string, each call to IsRequired() will throw a ValidationException. The message of each of these exceptions gets captured and then a single ValidationException will be called with the concatenated messages.

No comments:

Code Rant

Notepad, thoughts out loud, learning in public, misunderstandings, mistakes. undiluted opinions. I'm Mike Hadlow, an itinerant developer. I live (and try to work in) Brighton on the south coast of England. Please don't mistake me for an expert in anything. I love technology and programming, but make no claims to be any good at it. Much of what you read here may be poorly thought out, wrong, or just plain dangerous.