Manually trigger foolproof validation

May 3, 2013 at 4:59 PM
Edited May 3, 2013 at 5:08 PM
I'm using Foolproof Validation so I can use [RequiredIf] attributes on my view model. The problem is that I'd like to trigger validation within my code using the same logic outside a controller.

I've tried creating my own validation context and using Validatior.TryValidateObject; however, it fails with Foolproof's custom RequiredIf validator. Is there a way to take my model and validate it other than passing it to a controller?

Am I using the wrong approach?

Here is my code:
var draftModel = _draftHelper.LoadDraft(draftId);

var validationResults = new List<ValidationResult>();

var vc = new ValidationContext(draftModel, null, null);

var isValidDraft = Validator.TryValidateObject(draftModel, vc, 
                                               validationResults, true);
And the error I get is on the TryValidateObject line
System.NotImplementedException: The method or operation is not implemented.
I've also posted on StackOverflow, but I'll update both posts with the answer when I find it.