更改默认的" {0}字段是必需的" (最终的解决方案?) [英] Change default "The {0} field is required" (ultimate solution?)
问题描述
美好的一天!
我有我使用的登录表单以下ViewModel类:
使用System.ComponentModel.DataAnnotations;...公共类用户登陆:IDataErrorInfo的
{
[需要]
[显示名称(登录)]
公共字符串登录{搞定;组; } [需要]
[显示名称(密码)]
公共字符串密码{搞定;组; } [显示名称(记住我)
公共BOOL了rememberMe {搞定;组; } #区域IDataErrorInfo的成员 //这将是一个型号值误差
公共字符串错误
{
得到
{
如果(!WebUser.CanLogin(登录,密码))
{
返回Resources.ValidationErrors.InvalidLoginPassword;
}
其他
{
返回的String.Empty;
}
}
} //所有处理由DataAnnotation属性,只是一个存根接口
公共字符串此[字符串COLUMNNAME]
{
得到
{
返回的String.Empty;
}
}
#endregion}
这在的Global.asax
:
DefaultModelBinder.ResourceClassKey =BinderMessages;
ValidationExtensions.ResourceClassKey =BinderMessages;
资源文件 BinderMessages.resx
放在里面App_GlobalResources文件,它有两个键 InvalidPropertyValue
(工作)和 PropertyValueRequired
这不,给我默认消息。
问:是否有可能修改此消息,或者它绑DataAnnotations?
我发现这个职位很多,但没有解决方案。现在我只是退回到这一点:
[必需的(ErrorMessageResourceType = typeof运算(Resources.ValidationErrors),ErrorMessageResourceName =必需)]
您可以创建自定义 ValidationAttribute
扩展 RequiredAttribute标签
,并设置在那里的值。是这样的:
公共类MyRequiredAttribute:RequiredAttribute标签
{
公共MyRequiredAttribute()
{
ErrorMessageResourceType = typeof运算(Resources.ValidationErrors);
ErrorMessageResourceName =必需;
}
}
然后用自定义属性装饰你的模型。
默认消息被编译成汇编DataAnnotations在资源文件在 System.ComponentModel.DataAnnotations.Resources.DataAnnotationsResources.resources
是 RequiredAttribute_ValidationError = {0}字段是必需的。
。因此,要回答你的问题,是的,这消息DataAnnotations的一部分。
编辑: PropertyValueRequired
用于非可空类型的空值的错误。如下 PropertyValueInvalid
提到的用于类型转换错误。
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")]
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.
这篇关于更改默认的" {0}字段是必需的" (最终的解决方案?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!