I understand how to do the simple validations when the validation of one property doesn't depend on the values of others. But when it comes to validating property X based on property Y, I'm a little lost. Here's my contrived example:
public MyEnums.ShapeColor Color { get; set; } // enum: Red, Green, Blue
I want to enforce this business rule: Color is a required field only if the Shape is Triangle. Can that be done declaratively? What sort of attributes can be applied to the Color property to enforce my business rule?
You can also group validators together in a rule set. A rule set allows you to validate a complex object or graph by composing different validators of different types and applying them to elements in the object graph. Examples of these elements include fields,
properties, and nested objects.
By using the Validation Application Block, you can perform validation and create rule sets in the following three ways:
Using configuration
Using attributes
Using code
private void acceptButton_Click(object sender, EventArgs e)
{
Customer customer = new Customer
{
FirstName = firstNameTextBox.Text,
LastName = lastNameTextBox.Text,
SSN = ssnTextBox.Text,
Address = new Address
{
StreetAddress = streetAddressTextBox.Text,
City = cityTextBox.Text,
State = stateComboBox.Text,
ZipCode = zipCodeTextBox.Text
}
};
ValidationResults results =
this.alternativeValidation.Checked
? Validation.Validate(customer, "Alternative")
: Validation.Validate(customer);
if (!results.IsValid)
{
StringBuilder builder = new StringBuilder();
builder.AppendLine("Customer is not valid:");
foreach (ValidationResult result in results)
{
builder.AppendLine(
string.Format(
CultureInfo.CurrentCulture,
"{0}: {1}",
result.Key,
result.Message));
}
MessageBox.Show(
this,
builder.ToString(),
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
MessageBox.Show(
this,
"Processing customer '" + customer.FirstName + "'",
"Working",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
Member
1 Points
6 Posts
Help with Validator block
Nov 26, 2009 11:06 PM|KJTurner71|LINK
I understand how to do the simple validations when the validation of one property doesn't depend on the values of others. But when it comes to validating property X based on property Y, I'm a little lost. Here's my contrived example:
public MyEnums.ShapeType Shape { get; set; } // enum: Circle, Square, Triangle
public MyEnums.ShapeColor Color { get; set; } // enum: Red, Green, Blue
I want to enforce this business rule: Color is a required field only if the Shape is Triangle. Can that be done declaratively? What sort of attributes can be applied to the Color property to enforce my business rule?
Thanks!
Star
13042 Points
3174 Posts
Re: Help with Validator block
Nov 28, 2009 07:36 AM|sukumarraju|LINK
MSDN http://msdn.microsoft.com/en-us/library/dd140088.aspx. I copied the below from MSDN
You can also group validators together in a rule set. A rule set allows you to validate a complex object or graph by composing different validators of different types and applying them to elements in the object graph. Examples of these elements include fields, properties, and nested objects.
By using the Validation Application Block, you can perform validation and create rule sets in the following three ways:
Application Architecture Guide 2.0
My Blog
Twitter