Validation Application Block Hands-On Lab 1 Not Validating

Topics: Validation Application Block
Mar 16, 2011 at 3:58 PM


I downloaded this today and completed Lab 1 of the Validation Application Block.  When I tried to return the validation results for the Customer class I noticed that the validation of the sub-class (Address) was not performed.  Is there something missing in the lab code?

The Customer class is defined thus:

using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
namespace ValidationHOL.BusinessLogic
    public class Customer
        [StringLengthValidator(1, 25)]
        public string FirstName { get; set; }
        [StringLengthValidator(1, 25)]
        public string LastName { get; set; }
        public string SSN { get; set; }
        public Address Address { get; set; }

 Notice that the Address reference does not have any validator associated with it although the address class (shown below) itself does contain validators:

using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace ValidationHOL.BusinessLogic
    public class Address
        [StringLengthValidator(1, 50)]
        public string StreetAddress { get; set; }
        [StringLengthValidator(1, 30)]
        [ContainsCharactersValidator("sea", ContainsCharacters.All)]
        public string City { get; set; }
        [StringLengthValidator(2, 2)]
        public string State { get; set; }
        public string ZipCode { get; set; }

The validator is constructed as follows:

        private void MainForm_Load(object sender, EventArgs e)
            ValidatorFactory valFactory = EnterpriseLibraryContainer.Current.GetInstance<ValidatorFactory>();
            customerValidator = valFactory.CreateValidator<Customer>();

and the code for calling the validator is given here:

            ValidationResults results = customerValidator.Validate(customer);
            if (!results.IsValid)
                    "Customer is not valid",

I find that the results are always valid when the customer object is valid (regardless of whether the address object is valid or not).  How can I call the address validation as part of the customer validation too?

many thanks


Mar 16, 2011 at 7:27 PM
Edited Mar 16, 2011 at 7:30 PM


The use of the ObjectValidatorAttribute is deliberately left out of Lab 1, because validating object graphs is discussed in Lab 3. To allow the Address to be validated, at the [ObjectValidator] attribute to the Address property and you're good to go.


Mar 17, 2011 at 9:03 AM

Thanks dot_net_junkie, I thought it must be some missing attribute for the object.  I don't think it should be left out of Lab1 because otherwise it seems pointless putting the validation on the Address fields.