This project is read-only.

Is there a way to affect a rule's message?

Jul 16, 2009 at 2:45 PM

We currently use DataAnnotations for our server-side validation.  Prior to that we used the Validation Application Block (VAB).  VAB has a really excellent features when it comes to error messages in that it uses pre-defined tokens in the resource strings.  For example:

"Field {1} requires a value between {3} and {5}"

VAB populates the {1} token with the property name, the {3} with a lower-bound value, and the {5} with the upper-bound.

I was able to duplicate this in DataAnnotations by writing an extension method to overload the ValidationAttribute's FormatErrorMessage method.  Basically, my new signature is:  FormatErrorMessage(string propertyName, object value) whereas the original is: FormatErrorMessage(string value).

Unfortunately, I don't see a way to get xVal to utilize my extension, so now my client-side validation messages just have the resource string without any token replacements.  Will I have to get the source and force what I need or is there another way to achieve my goal?

Jul 16, 2009 at 3:54 PM

After reviewing the xVal source I can see this is going to be tricky, to say the least.  Perhaps you can indicate if I am even looking at the right approach.

First, I left of a token from my example above that changes the approach I take.  In VAB there is also a token {0}, which is the value being evaluated.  I can see that 1, 3, and 5 tokens can all be handled either in the DataAnnotationsRulesProvider's ApplyErrorMessage() or in the RuleConverter's Serialize() method.  But there is no way to deal with a 0 token if it is in the resource string.

I have almost no jQuery experience, but I imagine I will have to take the "rule.ErrorMessageOrResourceString" (in the Serialize() method) and turn it into some jQuery that can read the value entered.  Does this seem right?

Jul 16, 2009 at 4:43 PM

In an effort to avoid altering the xVal source (which can only lead to headaches down the road for us) I decided to try writing a new RuleProvider... the goal being to provide tokenized support for the the 1, 3, and 5 tokens as described above.

It looks as though I am still at an impasse though.  I don't see a way to get the field name to the provider.