Validation Application Block Hands-On Lab 1 Not Validating

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

Hello,

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; }
        [RegexValidator(@"^\d\d\d-\d\d-\d\d\d\d$")]
        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; }
        [ValidatorComposition(CompositionType.And)]
        [StringLengthValidator(1, 30)]
        [ContainsCharactersValidator("sea", ContainsCharacters.All)]
        public string City { get; set; }
        [StringLengthValidator(2, 2)]
        public string State { get; set; }
        [RegexValidator(@"^\d{5}$")]
        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)
            {
                MessageBox.Show(
                    this,
                    "Customer is not valid",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return;
            }

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

Claire

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

Claire,

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.

Cheers

Mar 17, 2011 at 8: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.

cheers