Validation w/ configuration file - Method property

Topics: General discussion, Validation Application Block
Mar 23, 2010 at 10:19 PM


I was curious as to how the "Method" and "Field" properties worked on the rule set. I tried seaching through the site here, but didn't turn up any useful results. Can anyone point me in the right direction or explain how to make use of these capabilities? Thanks.

Mar 24, 2010 at 6:28 AM


I’m a bit confused regarding your question. Could you provide more contexts about this?

For what I know, all data members (Properties, Methods and Fields) from your selected Object type in your Validation Application Block works/behave the same way in the rule set. It’s just in the type of validator being applied they may vary which is depend on your needs.

Btw, also note in Methods the return values are the one being validated.



Mar 24, 2010 at 9:41 AM

Ginkapitan is right. Field and method validators work the same as validators defined on properties. Methods however, are only validated when they have a return argument and no input arguments. Other methods decorated with validation attributes are just skipped.

Mar 24, 2010 at 8:35 PM

Alright this all helps. Now, is it possible if you have a method that lets say returns a integer; method has not parameters, to use the value returned from the method in the validation of a property I included on my ruleset? So I would have a property called "DistanceTraveled" which is a integer and use the value returned; integer, from my method to perform some kind of range or custom validation in the config. Is this feasible?

Mar 25, 2010 at 5:03 AM

On the first question - Yes, you'll need to include your method in your rule set and can directly implement your desired (custom or range) validator on it, where it validates the returned integer from your method.

For the second - I'm a bit off where "DistanceTraveled" came from. Assuming this is also a property came from your object, if you want to compare it with the returned integer from your method then you can try having a Property Comparison Validator from your method and assigning this "DistanceTraveled" as the PropertyToCompare.



Mar 25, 2010 at 9:36 AM

While it is technically possible to do this with a property validator, what you actually want to do is 'between fields' validation. The correctness of one property is dependent on the value of another property (or method) in your object to validate. For this scenario VAB supports a concept called self validation. See an example using attribute here. When using a configuration file to do this, you will need to create your own Validator and hook it up in the configuration (don't forget to put it under the self node!). Here is an example:

public sealed class MyValidator : Validator
    public MyValidator(NameValueCollection attributes)
        : base(string.Empty, string.Empty)

    protected override string DefaultMessageTemplate
        get { throw new NotImplementedException(); }

    protected override void DoValidate(object objectToValidate, 
        object currentTarget, string key, ValidationResults results)
        Customer customer = (Customer)currentTarget;

        var maxDistance = customer.CalculateMaxDistance();

        if (customer.DistanceTraveled > maxDistance)
            string message = "Distance travaled must be less then " + maxDistance;

            this.LogValidationResult(results, message, currentTarget, key);



Mar 25, 2010 at 1:12 PM

Thank you both for your help! I believe dot_net_junkie has explained to me exactly what I am looking  for. I will try to apply what you've shown me in this example and see if it works. Thanks again!