Validation Block Error Messages Order

Topics: Validation Application Block
Nov 23, 2009 at 9:10 AM


Is it possible to define the order in which the error messages will be displayed to user / retrived from validation.

Each time I restart My computer it "randomly" selects a new order. This is my property definition:

[RangeValidator(0, RangeBoundaryType.Inclusive, 359940, RangeBoundaryType.Inclusive, MessageTemplate = "Domain_WorkBlock_EndTimeInvalid")]
[NotNullValidator(MessageTemplate = "Domain_WorkBlock_EndTimeNull")]
public int? EndTime

Getting results with:

ValidationResults results = Microsoft.Practices.EnterpriseLibrary.Validation.Validation.Validate(_myObject);

In this example sometimes it shows/returns first the error message from Not Null Validator, other times it shows/returns first the Range Validator.

Regards, apestana.


Nov 24, 2009 at 4:25 AM


On what certain situation that you would encounter the change in the order of the validation messages? Does it happen for the same test value? Say, I always pass a null value for the EndTime property, will the order change. Please provide other information to repro the scenario.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Dec 15, 2009 at 6:07 PM

I supposed that the order in which the validator appears would define the validation results output order, but that is not the case.

The same definition in two different properties results in a different output:

[NotNullValidator(MessageTemplate = "Domain_Node_Name_Empty")]
[StringLengthValidator(0, 1, Negated = true, MessageTemplate = "Domain_Node_Name_Empty")]
[StringLengthValidator(1, 40, MessageTemplate = "Domain_Node_Name_Less40Chars")]
public string Name {...}

[NotNullValidator(MessageTemplate = "Domain_Node_ShortName_Empty")]
[StringLengthValidator(0, 1, Negated = true, MessageTemplate = "Domain_Node_ShortName_Empty")]
[StringLengthValidator(1, 10, MessageTemplate = "Domain_Node_ShortName_Less10Chars")]
public string ShortName {....}


errorProvider.ErrorMessages[0] -> Domain_Node_Name_Less40Chars, Domain_Node_Name_Empty

errorProvider.ErrorMessages[1] ->Domain_Node_ShortName_Empty, Domain_Node_ShortName_Less10Chars


Notice that the empty in [0] comes second








Dec 16, 2009 at 7:09 AM
Edited Dec 17, 2009 at 6:53 AM



I've successfully reproduce your scenario. Yes, the order is somehow altered, we are expecting that the Message from the [StringLengthValidator(0, 1, Negated = true, MessageTemplate = "Domain_Node_Name_Empty")] will come first. But i dont get it randomly. As far as I've debugged the code, on its internal implementation, the arrangement of the validators are altered but it's because of the way the MemberInfo gets the custom attributes.  A workaround for this is to use the config tool for the validation and create the validator just like the order in your attributes and it works correctly, the validation messages order are proper.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.