[MaxLength(45, ErrorMessage = Translations.Attribute.MAX_LENGTH)]
[Required(ErrorMessage = Translations.Attribute.REQUIRED)]
Run Code Online (Sandbox Code Playgroud)
如何使用默认的已翻译消息创建自定义Required和MaxLength验证。我可以简单地覆盖它并仅更改errorMessage吗?
我只想写
[MyMaxLength(45)]
[MyRequired]
Run Code Online (Sandbox Code Playgroud)
已建立所需解决方案:
public class MyRequiredAttribute : RequiredAttribute
{
public override string FormatErrorMessage(string name)
{
return string.Format("Polje {0} je obvezno", name);
}
}
Run Code Online (Sandbox Code Playgroud)
您应该能够从MaxLengthAttribute或任何其他正在使用的属性中得出...
public class MyMaxLengthAttribute : MaxLengthAttribute
{
public MyMaxLengthAttribute(int length) : base(length)
{
ErrorMessage = Translations.Attribute.MAX_LENGTH;
}
// other ctors/members as needed
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1808 次 |
| 最近记录: |