Custom Validator will not load in config tool

Jun 3, 2007 at 12:13 AM
Edited Jun 3, 2007 at 12:21 AM
Hi,

I have been trying to create a new custom validator: "RuleSetValidator"

The validator will validate the object being parsed as argument with a supplied rule set name. The purpose of this validator is to be used in conjunction with the OR compositional validators to perform more complex dynamic validation. For instance, a business contains a boolean value, which when true, should result in a specific validation to occur. What i want to do is decorate that boolean value with (pseudo code):

OR
DomainValidator(value=false)
RuleSetValidator(ruleset="ValidateOnlyWhenTrue")
public bool Value {}

I can then define the "ValidateOnlyWhenTrue" on the same type, which then validates whatever i want do have checked.

My problem is that I cannot get it to load in the config tool no matter what i try. The only way i can get it to show is if I decorate my validator with the attribute: ConfigurationElementType(typeof(CustomValidatorData)) instead of the intended: ConfigurationElementType(typeof(RuleSetValidatorData)). I want to use my own RuleSetValidatorData class in order to be able to select a target ruleset using the configuration tool.

Complete validator code shown below:
    [ConfigurationElementType(typeof(RuleSetValidatorData))]
    public class RuleSetValidator : Validator
    {
        private Validator differentRuleSetValidator = null;
 
               
     public RuleSetValidator()
            : base(null, null)
		{
		}
 
       public RuleSetValidator(NameValueCollection attributes)
            : base(null, null)
        {            
        }
                
        public RuleSetValidator(Type targetType, string targetRuleset)
            : base(null, null)
        {
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }
            if (targetRuleset == null)
            {
                throw new ArgumentNullException("targetRuleset");
            }
            differentRuleSetValidator = ValidationFactory.CreateValidator(targetType, targetRuleset);
        }
        
        protected override void DoValidate(object objectToValidate,
            object currentTarget,
            string key,
            ValidationResults validationResults)
        {
            if (objectToValidate != null)
            {
                differentRuleSetValidator.Validate(objectToValidate, validationResults);                
            }
        }
        
        protected override string DefaultMessageTemplate
        {
            get { return null; }
        }
    }
 



 
public class RuleSetValidatorData : ValidatorData
    {
        
        public RuleSetValidatorData()            
        { }
 
        
        public RuleSetValidatorData(string name)
            : base(name, typeof(RuleSetValidatorData))
        { }
 
        
        protected override Validator DoCreateValidator(Type targetType)
        {
            return new RuleSetValidator(targetType, this.TargetRuleset);
        }
        
        private const string TargetRulesetPropertyName = "targetRuleset";
        
        [ConfigurationProperty(TargetRulesetPropertyName)]
        public string TargetRuleset
        {
            get { return (string)this[TargetRulesetPropertyName]; }
            set { this[TargetRulesetPropertyName] = value; }
        }
    }
 
 

Jun 3, 2007 at 2:00 AM
You haven't done anything wrong; you're just not done yet. The node isn't showing up because you haven't build a ConfigurationNode for the config tool to show, nor have you added the nodes in the right places using the DesignManager.

Take a look at the existing validation.configuration.design dll for examples of existing configuration nodes. Adding a single one like this is fairly easy.

-Chris
Jun 3, 2007 at 7:22 AM
Thanks a bunch Chris! :)

This problem was driving me crazy...atleast now I know where to look.