I have a custom validation in the enterprise validation block. The DoValidate method is as shown below.
protected override void DoValidate(Double objectToValidate,
object currentTarget, string key, ValidationResults validationResults)
{
if (!IsSalMoreThanMinWage(objectToValidate))
{
//Here I need to mark this message as a "Warning"
LogValidationResult(validationResults,
"Salary is too low for this state", currentTarget, key);
}
}
I’d need to mark this validation failure as a “warning” message. In the front end, when I iterate through the ValidationResults collection and grab a ValidationResult object, I would need to identify and group different types of messages and render them differently.
My question is – how do I mark a failure as a warning?
You can use the
Tagproperty of theValidationResult. “The meaning for a tag is determined by the client code consuming the ValidationResults.”If you are using configuration, then you can specify the tag in your configuration file:
Or set the Tag with a property:
If you want to do it programmatically, then you will have to create a new validation result since the Tag property is readonly:
Then in the front end you can check the Tag property of the ValidationResult to see if it’s a warning:
Obviously, you can abstract this much better, aggregate the errors and warnings etc.
UPDATE
We don’t have identical requirements to yours but we do something similar. Unfortunately, the only way I know to execute the type of conditional validation you are talking about is to use RuleSets.
What we do is use a naming convention for the RuleSets and construct the RuleSet Names at runtime. If the RuleSet exists then we run the validator. You could do something similar for your warnings. So you could have two RuleSets:
And then retrieve a List of Validators based on whether you want to run the warning validation:
RuleSetType is an enum with different types of rules (e.g. Select, Insert, Update, Delete, PrimaryKey, etc.).