开发者

Custom validator not invoked when using Validation Application Block through configuration

开发者 https://www.devze.com 2022-12-15 22:13 出处:网络
I have set up a ruleset in my configuration file which has two validators, one of which is a built-inNotNullValidator, the other of which is a custom validator. The problem is that I see the NotNullVa

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">
        &l开发者_StackOverflow社区t;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


You custom validator should not be configured as a "property validator", but as a "self validator". Move the node up to directly under the node in your configuration. That should do the trick.


for custom validator you must set the required attribute of the field that you're setting the validator for to 'true', i.e required="true"

0

精彩评论

暂无评论...
验证码 换一张
取 消