How do I validate it?

Topics: Validation Application Block
Oct 1, 2008 at 8:40 PM
Edited Oct 2, 2008 at 6:45 PM
How do I validate a property using a custom validator where its validation involve another property?

I tried:
[MixedValidator(this.PropertyONE)]
PropertyTWO
{
            get { return strPropertyTWO; }
            set { strPropertyTWO = value; }
}

But I got this error: An object reference is required for the non-static field, method, or property.

Regards,
Roberto O Santos

Oct 2, 2008 at 8:50 PM
Does the PropertyComparisonValidator work in your scenario?
Oct 2, 2008 at 10:59 PM
Hello Spencer,

Actually that's not a comparison as instead one property is used on the math process to validate the other.

Oct 3, 2008 at 11:56 AM
Hi Roberto,

Attributes need to be resolved at compile time, so you cannot configure them with the value of an instance property.

Spencer's suggestion is appropriate; while the actual validation logic of this validator is not what you need, you do need to configure your validator in a similar way (ie providing a property name and using it at runtime to get the value). You can look at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.PropertyComparisonValidatorAttribute.DoCreateValidator for details.

Hope this helps,
Fernando
Oct 3, 2008 at 1:03 PM
Hey Fernando,

I didn't get the suggestion. Is the suggestion to create a custom PropertyComparisonValidatorAttribute?
I tried to google it but no answers were found.
I found a PropertyComparisonValidator example that shows how to use it. The attributes are a property and a ComparisonOperator.
Do you have an exemple about how create a custom ComparisonOperator?

Regards,
Roberto
Oct 3, 2008 at 1:28 PM
Roberto,

What I meant is that you need to create your own validator/validator attribute combo that works like the PropertyComparisonValidator in that it is configured with a property name and uses that when the validator is created to provide the means to access the related property's value. The method I pointed you too shows how the PropertyComparisonValidatorAttribute does this, and should be helpful to learn how to build your own.

Fernando
Oct 3, 2008 at 6:20 PM
I don't know if I have done something wrong but both validator and atribute have methods not implemented.

There is just an example above.

I tried to make something like:

On the attribute
public DigitAttributeValidator(string strVal)
        {
            this.Val= strVal;
        }

        protected override Validator DoCreateValidator(Type targetType)
        {
            return new DigitValidator(Val);
        }
On the validator
public DigitValidator(ValueAccess Val)
            : base(null, null)
        {
            this.Val = Val.ToString;
        }

But I got this error: Error    1    The best overloaded method match for 'DigitValidator(Microsoft.Practices.EnterpriseLibrary.Validation.ValueAccess)' has some invalid arguments.


Example
...
//
        // Summary:
        //     Creates the Microsoft.Practices.EnterpriseLibrary.Validation.Validator described
        //     by the attribute.
        //
        // Parameters:
        //   targetType:
        //     The type of object that will be validated by the validator.
        //
        //   ownerType:
        //     The type of the object from which the value to validate is extracted.
        //
        //   memberValueAccessBuilder:
        //     The Microsoft.Practices.EnterpriseLibrary.Validation.MemberValueAccessBuilder
        //     to use for validators that require access to properties.
        //
        // Returns:
        //     The created Microsoft.Practices.EnterpriseLibrary.Validation.Validator.
        protected override Validator DoCreateValidator(Type targetType, Type ownerType, MemberValueAccessBuilder memberValueAccessBuilder);
}
Oct 6, 2008 at 12:12 PM
Hello,

Thanks for everybody that help me. I've finally found the answer unfortunately I can't publish my code here as an example but if someone wants to validate a property using the value of another one, look at this example about Custom Property Comparison Validator: http://weblogs.asp.net/spano/archive/2007/05/16/developing-a-custom-property-comparison-validator-using-entlib-s-vap.aspx

There you'll find how to use the ValueAccess and Reflection to get the value that you need.

Thanks again guys!

Cya!