Quick Tip – Adding Business Validation Messages to ValidationSummary
Two days ago I was asked
how to insert business
validation messages into a
The post is the answer I
gave to the person who asked
We have a web form that is doing input validation and output the errors
inside a ValidationSummary control. Since ValidationSummary shows a
summary of all validation errors from ASP.NET validators how can we
add business validations which are part of the business logic we have to
the output of the ValidationSummary ?
Also, how can we make the input not valid according to the business logic?
We can use the CustomValidator control to our rescue. In code we will
create the CustomValidator and sets its IsValid property to false. We
also add our business error message to the ErrorMessage property.
Then we will add the created CustomValidator to the Page’s Validator
list. The following helper method will perform what I describe:
public static void AddBusinessErrorMessage(Page page, string message)
var validator = new CustomValidator();
validator.IsValid = false;
validator.ErrorMessage = message;
In the post I showed an example of using a CustomValidator in order
to add business error messages to a ValidationSummary control.
There are more solutions out there that will do the same thing but this
solution is the easiest to implement.