更改默认值“{0} 字段为必填项” (最终解决方案?)

发布于 2024-10-06 11:14:21 字数 1581 浏览 0 评论 0原文

再会!

我有以下用于登录表单的 ViewModel 类:

using System.ComponentModel.DataAnnotations;

...

public class UserLogin : IDataErrorInfo
{           
    [Required]
    [DisplayName("Login")]
    public string Login { get; set; }

    [Required]
    [DisplayName("Password")]
    public string Password { get; set; }

    [DisplayName("Remember Me")]
    public bool RememberMe { get; set; }

    #region IDataErrorInfo Members

    // This will be a Model-level error
    public string Error
    {
        get
        {
            if (!WebUser.CanLogin(Login, Password))
            {
                return Resources.ValidationErrors.InvalidLoginPassword;
            }
            else
            {
                return String.Empty;
            }
        }
    }

    // All is handled by DataAnnotation attributes, just a stub for interface
    public string this[string columnName]
    {
        get
        {
            return string.Empty;
        }
    }
    #endregion

}

这在 Global.asax 中:

DefaultModelBinder.ResourceClassKey = "BinderMessages";
ValidationExtensions.ResourceClassKey = "BinderMessages";

资源文件 BinderMessages.resx 放置在 App_GlobalResources 内,它有两个键 InvalidPropertyValue (有效)和 PropertyValueRequired 无效,并给我默认消息。

问题:是否可以修改此消息,或者它与 DataAnnotations 相关?

我发现了很多关于此的帖子,但没有解决方案。现在我只是回退到这个:

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

Good day!

I've the following ViewModel class I use for login form:

using System.ComponentModel.DataAnnotations;

...

public class UserLogin : IDataErrorInfo
{           
    [Required]
    [DisplayName("Login")]
    public string Login { get; set; }

    [Required]
    [DisplayName("Password")]
    public string Password { get; set; }

    [DisplayName("Remember Me")]
    public bool RememberMe { get; set; }

    #region IDataErrorInfo Members

    // This will be a Model-level error
    public string Error
    {
        get
        {
            if (!WebUser.CanLogin(Login, Password))
            {
                return Resources.ValidationErrors.InvalidLoginPassword;
            }
            else
            {
                return String.Empty;
            }
        }
    }

    // All is handled by DataAnnotation attributes, just a stub for interface
    public string this[string columnName]
    {
        get
        {
            return string.Empty;
        }
    }
    #endregion

}

And this in Global.asax:

DefaultModelBinder.ResourceClassKey = "BinderMessages";
ValidationExtensions.ResourceClassKey = "BinderMessages";

The resource file BinderMessages.resx is placed inside App_GlobalResources it has two keys InvalidPropertyValue (which works) and PropertyValueRequired which doesn't and gives me default message.

Question: Is it possible to modify this message, or it's tied to DataAnnotations?

I've found many posts about this, but without solution. For now I just fallback to this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断肠人 2024-10-13 11:14:21

您可以创建一个扩展 RequiredAttribute 的自定义 ValidationAttribute 并在其中设置值。类似于:

public class MyRequiredAttribute : RequiredAttribute
{
    public MyRequiredAttribute()
    {
         ErrorMessageResourceType = typeof(Resources.ValidationErrors);
         ErrorMessageResourceName = "Required";
    }
}

然后用您的自定义属性装饰您的模型。

默认消息编译到 System.ComponentModel.DataAnnotations.Resources.DataAnnotationsResources.resources 下资源文件中的 DataAnnotations 程序集中,并且 RequiredAttribute_ValidationError=The {0} 字段是必需的。。所以回答你的问题,是的,该消息是 DataAnnotations 的一部分。

编辑:PropertyValueRequired 用于处理不可为空类型的空值错误。如下所述,PropertyValueInvalid 用于类型转换错误。

You can create a custom ValidationAttribute that extends RequiredAttribute and sets the values there. Something like:

public class MyRequiredAttribute : RequiredAttribute
{
    public MyRequiredAttribute()
    {
         ErrorMessageResourceType = typeof(Resources.ValidationErrors);
         ErrorMessageResourceName = "Required";
    }
}

Then decorate your Model with your custom attribute.

The default message is compiled into the DataAnnotations assembly in the resource file under System.ComponentModel.DataAnnotations.Resources.DataAnnotationsResources.resources and is RequiredAttribute_ValidationError=The {0} field is required.. So to answer your question, yes, that message is part of DataAnnotations.

Edit: PropertyValueRequired is used for errors on null values with non-nullable types. As mentioned below PropertyValueInvalid is used for type conversion errors.

提笔书几行 2024-10-13 11:14:21

我已经完成了一种使用单例类来提供翻译的方法。您仍然需要按照 @bmancini 的建议派生所有属性​​。我的方法的优点是您可以使用多个字符串表(或切换翻译源),而无需修改任何其他逻辑。

由于我的博客文章比较大,我只提供一个链接:
http://blog.gauffin.org/2010/11/simplified-数据注释本地化/

I've done an approach using a singleton class to provide the translations. You still need to derive all attributes as suggested by @bmancini. The upside with my approach is that you can use multiple string tables (or switch translation source) without having to modify any other logic.

Since my blog entry is rather large, I'll just provide a link:
http://blog.gauffin.org/2010/11/simplified-localization-for-dataannotations/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文