VAB Object Validator results and configuration problem.

Topics: Validation Application Block
Mar 20, 2007 at 1:58 PM
Hi

I'm trying to use Object Validator, with a configuration file.

After modify the message property in Visual Studio (for the object validator in app.config) and save the configuration file, the message is not saved.

When I run the validate method, the validators inside the object are validated, but the object validator is not added to results.

This is by design?

TIA
Mar 20, 2007 at 9:30 PM
By message not being saved, I assume you mean the MessageTemplate.

If the MessagegeTemplate is not being saved then you should be receiving a configuration error. The Configuration Editor validates the configuration before saving ( you can also validate separately via the Action Menu ) and displays errors. I would double-check your configuration.

More importantly, however, ObjectValidator ValidationResults will be added to the ValidationResults of the main object being validated.

For example, if I have a Customer Class that contains and Address Class:

public class Customer
{
    private string _emailAddress;
    private Address _address;
 
    [StringLengthValidator(1,100,
        MessageTemplate = "Email Address Invalid Length")]
    public string EmailAddress
    {
        get { return _emailAddress; }
        set { _emailAddress = value; }
    }
 
    [ObjectValidator()]
    public Address Address
    {
        get { return _address; }
        set { _address = value; }
    }
}

public class Address
{
    private string _zipCode;
 
    [StringLengthValidator(1,10,
		MessageTemplate = "ZipCode Invalid Length")]
    public string ZipCode
    {
        get { return _zipCode; }
        set { _zipCode = value; }
    }
}

When I validate a Customer instance using the following code:

class Program
{
    static void Main(string[] args)
    {
        Address address = new Address();
 
        // Invalid Length
        address.ZipCode = "123456789123456789";
 
        Customer customer = new Customer();
 
        // Invalid Length
        customer.EmailAddress = "";
 
        customer.Address = address;
 
        ValidationResults results = Validation.Validate<Customer>(customer);
 
        if (!results.IsValid)
        {
            foreach (ValidationResult result in results)
                Console.WriteLine("Error: {0}", result.Message);
 
            Console.ReadLine();
        }
    }
}

I get the validation errors for both the Customer and the Address:

Error: Email Address Invalid Length
Error: ZipCode Invalid Length

I double-checked it works the same via XML Configuration as well.

Regards,

Dave

______________________

David Hayden
Microsoft MVP C#
Mar 21, 2007 at 1:47 PM
Hi, thanks for your help!

I can see the validation results from child objects inside the main object, but I can't save a message template for the ObjectValidator, and the ObjectValidator is not added to the validationresults (yes, the validation results for child objects are added)

If I use the Validate inside the object menu I don't see any error.

I'm using only XML Configuration
Mar 21, 2007 at 2:45 PM
Oh, I see what you mean.

Maybe I am misunderstanding the ObjectValidator, but I think that is how it works. It delegates validation to the child validators and just includes their results in the ValidationResults.

I don't believe the ObjectValidator does any validation of its own and hence has any real need for the MessageTemplate, etc. You can't assign validation rules to ObjectValidator so I don't know how it would actually validate on its own.

The only reason you see the MessageTemplate Properties is because ObjectValidator inherits from Validator which has those properties by default. You are correct in that it doesn't save, and I think that is because it doesn't need the information because it doesn't do any validation itself.

Somebody correct me if I am wrong as I just assumed this little bit of knowledge and can't really find anything in the documentation.

Regards,

Dave

________________________

David Hayden
Microsoft MVP C#
Mar 21, 2007 at 6:38 PM
Hi,

How would I validate a Customer which has a collection of addresses in them:

public class Customer
{
private string _emailAddress;
private Collection<Address> _homeAddresses;

[StringLengthValidator(1,100,
MessageTemplate = "Email Address Invalid Length")]
public string EmailAddress
{
get { return _emailAddress; }
set { _emailAddress = value; }
}

ObjectValidator()
public Collection<Address> HomeAddresses
{
get { return _homeAddresses; }
set { _homeAddresses = value; }
}
}

In the code above, HomeAddresses property is actually a collection of Address objects. In this case how do I validate all the individual addresses and then get a composite ValidationResults object would contains the validation issues of the Customer object as well as the Address objects within it.

Thanks a lot for you time and reply!
Mar 21, 2007 at 6:56 PM
Are you tried Object Collection Validator?
Mar 21, 2007 at 8:45 PM
For a collection you would use the ObjectCollectionValidator as mentioned above.

Your example would look like:

public class Customer
{
	private string _emailAddress;
	private Collection<Address> _homeAddresses;
 
	[StringLengthValidator(1,100,
		MessageTemplate = "Email Address Invalid Length")]
	public string EmailAddress
	{
		get { return _emailAddress; }
		set { _emailAddress = value; }
	}
 
	[ObjectCollectionValidator(typeof(Address))]
	public Collection<Address> HomeAddresses
	{
		get { return _homeAddresses; }
		set { _homeAddresses = value; }
	}
}

Regards,

Dave

______________________

David Hayden
Microsoft MVP C#