WPF Data Validation and VAB

Topics: Validation Application Block
Jan 25, 2009 at 7:29 PM

Hi

I am thinking of using VAB as base for validating my business entities. In that case, it would also be neat to use same rules for validating user input. WPF/XAML has built in functions for data validation but how do I combine those with VAB?

Googling gives a couple of choses, http://www.codeplex.com/entlibcontrib/Release/ProjectReleases.aspx?ReleaseId=7637, and http://www.bennedik.de/2007/05/update-of-wpf-integration-for.html. There are maby more solutions out there?

Anyone who can give mee any tips, other solutions, pros and cons.... on this topic.

 

//lasse

Feb 2, 2009 at 6:12 AM
Edited Feb 5, 2009 at 10:48 AM
I can't give you any since I'm not that knowledgeable on WPF but here's an updated WPF integration with 4.0 by Martin Bennedick. http://www.pnpguidance.net/Post/WPFValidationApplicationBlockVABIntegrationUpdatedEnterpriseLibrary4.aspx


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Feb 6, 2009 at 1:52 PM
Hi
I've tested a couple solutions that I found on internet (among them your suggestion),,,, but one worked without problems for me,,, My guess is that I am using XAML in a way the authors not have expected.

I decided to go for a simple solution using the IDataErrorInfo interface. So my solution is to add IDataErrorInfo interface to all my business models. I have done it like

// Part of the IDataErrorInfo interface
public string this[string columnName]
{
get { return CheckProperty(columnName); }
}

 

// Helper
private
string CheckProperty(string name)
{
ValidationResults results = Microsoft.Practices.EnterpriseLibrary.Validation.Validation.Validate(this,name);
string resultString;
if (results.IsValid)
    resultString=
"";
else
{
    resultString =
"";
    
foreach(ValidationResult result in results)
    {
        resultString = resultString + result.Message +
"\n";
    }
}
return resultString;
}

 

The CheckProperty is called with the name of the property to chek as input.  Each property to be checked is setup like:

[

StringLengthValidator(3, 50, Ruleset = "Signum", MessageTemplate = "Signum must be between 1 and 50 characters")]
public string Signum {...}

 

The important thing is to create a ruleset with the same name as the Property, to enable the validation of the rule...

ValidationResults results = Microsoft.Practices.EnterpriseLibrary.Validation.Validation.Validate(this,name);

This is the only way I found to specify specific property to validate,,, maby there is a more elegant solution ??? if so,,, I would be glad to hear about it.

//lassse
Feb 9, 2009 at 9:09 AM
There's other way I can think of right now but yours is more flexible.  What I'm thinking involves creating a specific validator and passing to its Validate method the property you want to validate.  Of course yours is more elegant because you can simply modify the config if you want to change the validator.  The other approach would require you to modify your code if you want to change your validator.


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