In my MVC application I have the ability to get the error message from a text file instead of using the default error message. This works perfectly on the Required attribute (both Serverside and Clientside).
I now need to do the same with the Compare attribute, but I can’t figure out how to override the Compare attribute.
For reference, this is how I am doing it with the Required attribute (I would like similar code to this to work with the Compare attribute)…
Public Class RequiredFieldAttribute
Inherits ValidationAttribute
Implements IClientValidatable
Private innerAttribute As New RequiredAttribute()
Private errormessagecontrolid As String
Public Sub New(ErrorMessageControlID As String)
Me.errormessagecontrolid = ErrorMessageControlID
End Sub
Protected Overrides Function IsValid(value As Object, validationContext As ValidationContext) As ValidationResult
If Not innerAttribute.IsValid(value) Then
Return New ValidationResult(ErrorMsgs.Text(Me.errormessagecontrolid))
End If
Return ValidationResult.Success
End Function
Public Function GetClientValidationRules(metadata As ModelMetadata, context As ControllerContext) As IEnumerable(Of ModelClientValidationRule) Implements IClientValidatable.GetClientValidationRules
Dim result = New List(Of ModelClientValidationRule)
Dim rule = New ModelClientValidationRule() With {.ErrorMessage = ErrorMsgs.Text(Me.errormessagecontrolid), .ValidationType = "required"}
result.Add(rule)
Return result
End Function
End Class
Above, ErrorMsgs.Text is the function that retieves the message from the text file. Against my model I then apply something like this…
<RequiredField("AccountDetailsPostcodeError")>
Public Property PostCode As String
The system then looks in the Text file for an entry called AccountDetailsPostcodeError.
How can I achieve the same with the Compare attribute. At the moment I have a hard coded error message like this…
<Compare("WebPassword", ErrorMessage:="The password and confirmation do not match.")>
Public Property ConfirmWebPassword As String
Edit: The suggested fix below may work in C#, but won’t work in VB.NET, hence my more complex requirement to override the Compare attribute. I just don’t know how to correctly override it.
Why use a text file for your translations and messages, .NET has build in options for translations. You can use resources. The advantage of using resources is that resources are type safe, they are checked as compile time. Where your textfile can become corrupt / missing.
The following guide helps you with setting up resources in a Mvc project:
Step one
Edit the default assembly language:
Set this language to your default language. (For this example I use
English (United States))Step two
Add a resource file to your project. Call this file

Resource.resx. Open this file. Change the Access Modifier toPublicand start adding resource strings. For example:Step three
Add for each other language you want to support another resource file but name them
Resource.LANGUAGE.resxwhere LANGUAGE is replaced by the other culture name. For culture names you can check this url: http://msdn.microsoft.com/en-us/goglobal/bb896001.aspxThen fill the new resource file with the localized strings. For example:
Step four
Then you can in your Models use the default localization support of the attributes:
For example:
VB:
C#
For localization the attribute needs to know the name of the static property and the type of the static class where to get the property from (as seen above).
Step five
Then in your view use the
@Html.ValidationSummary()to get all the error messages, or use@Html.ValidationMessageFor(Function(model) model.Property)@Html.ValidationMessageFor(m => m.Property)to get specific error messages.
For the display attribute you can use:
@Html.DisplayNameFor(Function(model) model.Property)@Html.DisplayNameFor(m => m.Property)Changing the language
And last but not least you can change the language of your app instead of your neutral language defined in step one by editing the
Web.configand changing the globalization tag like so:If you want to change the language from code you should edit
System.Threading.Thread.CurrentThread.CurrentUICulturefor information about this I suggest google or another SO question.Example project
For this question I quickly made an example project to provide an accurate answer. Project can be found here:
MvcVBTest.V1.zip
UPDATE
If you don’t want to use Resources but a single text file you can use the same concept the resource framework uses. You need a class that has static properties you can reference.
For this purpose I did the following things:
Resources(Resources.vb).Resourceresource.xmlwhich I have mapped to an array ofResourceDictionary(Of String, String)ResourceTypeparameter in theUserModelclassAnd of course a little clean up. The old resources can be deleted and the
globalizationtag can be removed from theweb.config.Now all the text can be found in
resource.xmlas key value pairs. To add another line, add it to the XML and create a property for it in theResourceclass.Example project
For this update I updated my example project:
MvcVBTest.V2.zip