全球定位验证 [英] Globally localize validation

查看:160
本文介绍了全球定位验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用System.ComponeneModel.DataAnnotations为Required和StringLength属性等。是否有可能在全球范围内的本地化错误信息?

I'm using System.ComponeneModel.DataAnnotations attributes such as Required and StringLength. Is it possible to localize its error messages globally?

我知道我能做到这一点。

I know I can do this

[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Validation))]

但这样做我到处使用所需的属性将只是疯了。此外,我想避免的东西,如:

But doing this everywhere I use required attribute would be just insane. Also I'd like to avoid stuff like:

public class LocalizedRequiredAttribute : RequiredAttribute {
    public LocalizedRequiredAttribute()
        : base() {
        ErrorMessageResourceName = "Required";
        ErrorMessageResourceType = typeof(Resources.Validation);
    }
}

(但如果没有任何其他方式,我会满足于这一点)

(but if there isn't any other way, I'll settle for this)

推荐答案

据我所知,你需要使用一个自定义属性或指定ErrorMessageResourceName和ErrorMessageResourceType属性。这里有详细的另一种可能性 的:

AFAIK you need either a custom attribute or specify the ErrorMessageResourceName and ErrorMessageResourceType properties. There is another possibility detailed here:

创建一个全球性的资源类
  App_GlobalResources文件,并设置
  DefaultModelBinder.ResourceClassKey到
  这个类的名称(例如,
  如果您做了Messages.resx,然后设置
  ResourceClassKey为信息)。

Create a global resource class in App_GlobalResources, and set DefaultModelBinder.ResourceClassKey to the name of this class (for example, if you made "Messages.resx", then set ResourceClassKey to "Messages").

有两个字符串就可以覆盖
  在MVC 2:

There are two strings you can override in MVC 2:


      
  • 对于字符串值
      PropertyValueInvalid时使用
      数据输入的用户是不
      与数据类型兼容(用于
      例如,键入ABC为
      整场)。默认消息
      这是:值{0}不是
      有效期为{1}。

  • The string value for "PropertyValueInvalid" is used when the data the user entered isn't compatible with the data type (for example, typing in "abc" for an integer field). The default message for this is: "The value '{0}' is not valid for {1}."

对于字符串值
  PropertyValueRequired时使用
  用户没有输入任何一个数据
  字段,不能为空(对于
  例如,一个整数字段)。该
  此默认消息是:值
  是必需的。

The string value for "PropertyValueRequired" is used when the user did not enter any data for a field which is not nullable (for example, an integer field). The default message for this is: "A value is required."

要注意这一点很重要
  如果您有一个,第二个案例
  该
  DataAnnotationsModelValidatorProvider
  在您的验证提供商列表
  (这是默认设置),那么你
  将永远不会看到第二个消息。
  该供应商认为非可选字段
  并增加了一个隐含的[必填]
  赋予他们让自己
  消息将与其他一致
  与明确的领域[必填]
  属性并确保您得到
  所需客户端验证
  领域。

It's important to note in the second case that, if you have the DataAnnotationsModelValidatorProvider in your validator providers list (which it is by default), then you will never see this second message. This provider sees non-optional fields and adds an implied [Required] attribute to them so that their messages will be consistent with other fields with explicit [Required] attributes and to ensure that you get client-side validation for required fields.

这篇关于全球定位验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆