SelfValidation Not Working with Winforms Integration

Topics: Validation Application Block
Jan 6, 2009 at 3:20 PM
Hi Guys,

I've created a class to handle validating user interface input, once class per form section. There's no per field validation in one class, instead I've put the logic inside the class as self validtion as the rules are complicated. Unfortunately, this just doesn't work, when i run the validation the object isn't constructed, nor is any of the validation run. This is confusing to me, as i was believed the process for the validator was to create the class, populate it, and then supply it to the validator and process the response.

My Validated Class:

    public class MediaSearchParameters
        public MediaSearchParameters()
            //TEST: Never Thrown - Object Not Constructed
            throw new NotImplementedException();

        public DateTime? AddedMin { get; set; }

        public DateTime? AddedMax { get; set; }

        public DateTime? UpdatedMin { get; set; }

        public DateTime? UpdatedMax { get; set; }

        public int? LengthMin { get; set; }

        public int? LengthMax { get; set; }

        public int? SizeMin { get; set; }

        public int? SizeMax { get; set; }

        public List<string> SearchTerms { get; set; }

        public void ValidateAddedRange(ValidationResults results)
            if ((AddedMin != null) && (AddedMax != null) && (AddedMin > AddedMax))
                results.AddResult(new ValidationResult("Min Added date cannot be greater than Max Added Date", this, "AddedMin", null, null));

        public void ValidateUpdatedRange(ValidationResults results)
            if ((UpdatedMin != null) && (UpdatedMax != null) && (UpdatedMin > UpdatedMax)
                results.AddResult(new ValidationResult("Min Updated date cannot be greater than Max Updated Date", this, "UpdatedMin", null, null));

        public void ValidatedLengthRange(ValidationResults results)
            if ((LengthMin != null) && (LengthMax != null) &&(LengthMin > LengthMax))
                results.AddResult(new ValidationResult("Min Length cannot be greater than Max Length", this, "LengthMin", null, null));

        public void ValidateSizeRange(ValidationResults results)
            if (((SizeMin == null) && (SizeMax == null) && (SizeMin > SizeMax))
                results.AddResult(new ValidationResult("Min Size cannot be greater than Max Size", this, "SizeMin", null, null));

        public void ValidateNotEmpty(ValidationResults results)
            if (
                    (AddedMin == null) && (AddedMax == null) &&
                    (UpdatedMin == null) && (UpdatedMax == null) &&
                    (LengthMin == null) && (LengthMax == null) &&
                    (SizeMin == null) && (SizeMax == null) &&
                    (SearchTerms.Count == 0)
                results.AddResult(new ValidationResult("No Search criteria specified.", this, "SearchTerms", null, null));

As a note, i have added attributes to the properties in this class, and validation is performed, but again, the constructor is not called.

Any thoughts?


Jan 7, 2009 at 1:30 AM
SelfValidation are triggered when a validation is performed on the class containing it.  In your case for example, you would create first a MediaSearchParameters object.

MediaSearchParameters m = new MediaSearchParameters();
//assign values to properties.
ValidationResults results = Validation.Validate<MediaSearchParameters>(m); // this will trigger the self validation methods.

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.