Using RegularExpressionAttribute in a Custom ModelValidatorProvider
-
31-05-2021 - |
Question
I've got a custom implementation of ModelValidatorProvider
that I'm using in an ASP.NET MVC App. But I'm currently getting an error on any property that yields a RegularExpressionAttributeAdapter
. The error is Either ErrorMessageString or ErrorMessageResourceName must be set, but not both.
Now my code doesn't set the resource name but does set a message.
yield return new RegularExpressionAttributeAdapter(metadata, context, new RegularExpressionAttribute(myExpression) {
ErrorMessage = expressionErrorMessage
});`
I've tried setting both the ErrorMessageResourceName
to null and setting the ErrorMessageResourceType
to null but it doesn't help. If I don't set the ErrorMessage
the exception goes away but then I loose my user feedback for the validation.
Now, I should note that this used to work but I've recently upgraded the app to use MVC4 instead of MVC3 and I'm curious if this is unique to MVC4 or if it's something else?
Solution
While I never found out why it was doing this I was able to uncomment my code for this after upgrading to MVC4 and it works as expected so I can only assume it was something specific to the previous version.