Castle Validator Enhancements
I have contributed a couple of enhancements to Castle.Components.Validator that have been committed to the trunk. Besides using attributes, validations can now be supplied in code using the [ValidateSelf] attribute:
Each of the above validations *could* be done by using an attribute and a custom validator, but expressing validations in code is much simpler for these types of one off validations. You can have as many methods decorated with [ValidateSelf] on an object you want as long as they have the above method signature (void return and one ErrorSummary parameter). You can also specify the RunWhen and ExecutionOrder just like regular validators.
The second enhancement is the IValidationContributor interface. This allows you contribute to the validation of an object beyond the default validation. The interface is fairly simplistic:
You can extend AbstractValidationContributor so that you can perform initialization for a given type. The SelfValidationContributor implements the logic for recognizing and executing the self validation feature above. You can write custom contributors that can be injected into the DefaultValidatorRunner for things like retrieving validations from the container and invoking them on the object.
Enjoy!


Castle ActiveRecord: ValidateIsUnique Vs. Testing:
[...] and speaking of validation, Eric just put some really bad-ass new validation stuff into the Castle trunk. Now I have to pull castle trunk again. Grrr, damn you Castle for being so [...]
September 11, 2008, 10:22 amPascal Lindelauf:
Great work, Eric! That was just was I was looking for.
I tried to incorporate it in a bit of my code, but until now I got no further than calling the validation directly myself with the SelfValidationContributor. I expected the validation to fire off by itself on save or update as well, but it doesn’t. Are my expectations wrong or am I forgetting something?
Cheers,
Pascal.
October 9, 2008, 8:35 amehauser:
Sorry for the delay, I think a wordpress plugin disabled my email notifications. The validation should fire by itself. I’ve been using this for awhile without issues. If you want to send me some sample code, I can take a look at it.
November 2, 2008, 12:09 pmxVal - a validation framework for ASP.NET MVC « Steve Sanderson’s blog:
[...] version includes rules providers for .NET 3.5’s built-in DataAnnotations attributes as well as Castle Validator attributes, but you can also write your own IRulesProvider to attach rules programmatically or [...]
January 10, 2009, 10:23 am