Custom validator not invoked when using Validation Application Block through configuration
Posted
by Chris
on Stack Overflow
See other posts from Stack Overflow
or by Chris
Published on 2010-01-07T17:14:06Z
Indexed on
2010/04/14
1:43 UTC
Read the original article
Hit count: 463
I have set up a ruleset in my configuration file which has two validators, one of which is a built-in NotNullValidator, the other of which is a custom validator. The problem is that I see the NotNullValidator hit, but not my custom validator.
The custom validator is being used to validate an Entity Framework entity object.
I have used the debugger to confirm the NotNull is hit (I forced a failure condition so I saw it set an invalid result), but it never steps into the custom one.
I am using MVC as the web app, so I defined the ruleset in a config file at that layer, but my custom validator is defined in another project. However, I wouldn't have thought that to be a problem because when I use the Enterprise Library Configuration tool inside Visual Studio 2008 it is able to set the type properly for the custom validator. As well, I believe the custom validator is fine as it builds ok, and the config tool can reference it properly.
Does anybody have any ideas what the problem could be, or even what to do/try to debug further?
Here is a stripped down version of my custom validator:
[ConfigurationElementType(typeof(CustomValidatorData))]
public sealed class UserAccountValidator : Validator
{
public UserAccountValidator(NameValueCollection attributes)
: base(string.Empty, "User Account")
{
}
protected override string DefaultMessageTemplate
{
get { throw new NotImplementedException(); }
}
protected override void DoValidate(object objectToValidate,
object currentTarget, string key, ValidationResults results)
{
if (!currentTarget.GetType().Equals(typeof(UserAccount)))
{
throw new Exception();
}
UserAccount userAccountToValidate = (UserAccount)currentTarget;
// snipped code ...
this.LogValidationResult(results,
"The User Account is invalid", currentTarget, key);
}
}
Here is the XML of my ruleset in Validation.config (the NotNull rule is only there to force a failure so I could see it getting hit, and it does):
<validation>
<type defaultRuleset="default" assemblyName="MyProj.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="MyProj.Entities.UserAccount">
<ruleset name="default">
<properties>
<property name="HashedPassword">
<validator negated="true" messageTemplate="" messageTemplateResourceName=""
messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Not Null Validator" />
</property>
<property name="Property">
<validator messageTemplate="" messageTemplateResourceName=""
messageTemplateResourceType="" tag="" type="MyProj.Entities.UserAccountValidator, MyProj.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Custom Validator" />
</property>
</properties>
</ruleset>
</type>
</validation>
And here is the stripped down version of the way I invoke the validation:
var type = entity.GetType()
var validator = ValidationFactory.CreateValidator(type, "default", new FileConfigurationSource("Validation.config"))
var results = validator.Validate(entity)
Any advice would be much appreciated!
Thanks,
Chris
© Stack Overflow or respective owner