MVC - Change Compare attribute error message

前端 未结 2 446
有刺的猬
有刺的猬 2020-12-17 06:47

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 attribut

相关标签:
2条回答
  • 2020-12-17 07:11

    Why not use something like this?

    <RequiredField(ErrorMessage=GetErrorMessage())>
    

    Just create a static function that gets the error message for you. You can even take a parameter for your GetErrorMessage function so you can determine which message you'll want to return.

    0 讨论(0)
  • 2020-12-17 07:30

    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

    0 讨论(0)
提交回复
热议问题