SelfValidation - Removing/Modifying ValidationResults

Jul 31, 2009 at 6:08 AM

When using Self Validation we create and tag a method with the [SelfValidation] attribute. My question is whether it's possible to remove or somehow modify the validation results we receive as an argument. For example I would like to make the results of certain validators to be valid or invalid, as follows:

    [HasSelfValidation]
    public class Profile
    {
        [SelfValidation]
        public void SelfValidate(ValidationResults validationResults)
        {
            // if firstname = 'patterns' firstname should not be valid
        }

        [StringLengthValidator(1, 255, MessageTemplate = "Specify a firstname")]
        public string Firstname { get; set; }

        [StringLengthValidator(1, 255, MessageTemplate = "Specify a lastname")]
        public string Lastname { get; set; }
    }

 

 

 

Jul 31, 2009 at 6:38 AM

You can't remove items from the ValidationResults object.  If your use case is just as simple as the example above, you can add a [DomainValidator("patterns", Negated=true)] attribute in the FirstName property.  If you have other complex conditional validations, I'd suggest you just remove validators from the property you'll be implementing other custom validation(s) in a SelfValidation method.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 31, 2009 at 7:55 AM

Our case is that one class contains properties for two different things related to payment methods. If the user selects one value some fields should be validated, if the other value is selected a different set of values should be validated. I'm hoping there's some way I can override the ValidationResults to make this happen.

Jul 31, 2009 at 8:07 AM

Sounds like you should create two different classes, you should then be able to implement your validation in a very straightforward manner.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com