Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

asp.net mvc - MVC - Change Compare attribute error message

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  • (C#) Properties > Assembly information > Neutral Language
  • (VB) My Project > Assembly information > Neutral 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 to Public and start adding resource strings. For example: Default English resources

Step three

Add for each other language you want to support another resource file but name them Resource.LANGUAGE.resx where LANGUAGE is replaced by the other culture name. For culture names you can check this url: http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx

Then fill the new resource file with the localized strings. For example: English Resources

Step four

Then you can in your Models use the default localization support of the attributes:

For example:

VB:

Imports System.ComponentModel.DataAnnotations
Public Class UserModel
    <Display(Name:="UserNameField", ResourceType:=GetType(My.Resources.Resource))>
    <Required(AllowEmptyStrings:=False, ErrorMessageResourceName:="RequiredUsername", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
    Public Property UserName As String

    <Display(Name:="PasswordField", ResourceType:=GetType(My.Resources.Resource))>
    <MinLength(6, ErrorMessageResourceName:="PasswordLengthError", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
    <Compare("PasswordAgain", ErrorMessageResourceName:="CompareError", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
    <Required(AllowEmptyStrings:=False, ErrorMessageResourceName:="RequiredPassword", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
    Public Property Password As String

    <Display(Name:="PasswordAgainField", ResourceType:=GetType(My.Resources.Resource))>
    <Required(AllowEmptyStrings:=False, ErrorMessageResourceName:="RequiredPasswordAgain", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
    Public Property PasswordAgain As String
End Class

C#

using System.ComponentModel.DataAnnotations;
public class UserModel
{
    [Display(Name = "UserNameField", ResourceType = typeof(My.Resources.Resource))]
    [Required(AllowEmptyStrings = False, ErrorMessageResourceName = "RequiredUsername", ErrorMessageResourceType = typeof(My.Resources.Resource))]
    public string UserName;

    [Display(Name = "PasswordField", ResourceType = typeof(My.Resources.Resource))]
    [MinLength(6, ErrorMessageResourceName = "PasswordLengthError", ErrorMessageResourceType = typeof(My.Resources.Resource))]
    [Compare("PasswordAgain", ErrorMessageResourceName = "CompareError", ErrorMessageResourceType = typeof(My.Resources.Resource))]
    [Required(AllowEmptyStrings = False, ErrorMessageResourceName = "RequiredPassword", ErrorMessageResourceType = typeof(My.Resources.Resource))]
    public string Password;

    [Display(Name = "PasswordAgainField", ResourceType = typeof(My.Resources.Resource))]
    [Required(AllowEmptyStrings = False, ErrorMessageResourceName = "RequiredPasswordAgain", ErrorMessageResourceType = typeof(My.Resources.Resource))]
    public string PasswordAgain;
}

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

  • VB @Html.ValidationMessageFor(Function(model) model.Property)
  • C# @Html.ValidationMessageFor(m => m.Property)

to get specific error messages.

For the display attribute you can use:

  • VB @Html.DisplayNameFor(Function(model) model.Property)
  • C# @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.config and changing the globalization tag like so:


<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <globalization uiCulture="nl" />
  </system.web>
</configuration>

If you want to change the language from code you should edit System.Threading.Thread.CurrentThread.CurrentUICulture for 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:

  1. I created a class called Resources (Resources.vb).
  2. In this class I added a sub class called Resource
  3. In the static constructor of this class I open resource.xml which I have mapped to an array of Resource
  4. This array is then converted to an Dictionary(Of String, String)
  5. I created an static get property for every item in the xml. And returned the right item from the Dictionary
  6. I changed the ResourceType parameter in the UserModel class

And of course a little clean up. The old resources can be deleted and the globalization tag can be removed from the web.config.

Now all the text can be found in resource.xml as key value pairs. To add another line, add it to the XML and create a property for it in the Resource class.

Example project

For this update I updated my example project:
MvcVBTest.V2.zip


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...