gpt4 book ai didi

c# - 如何使用新错误消息创建自定义最大长度和必需验证,逻辑保持不变

转载 作者:行者123 更新时间:2023-11-30 14:10:11 26 4
gpt4 key购买 nike

[MaxLength(45, ErrorMessage = Translations.Attribute.MAX_LENGTH)]
[Required(ErrorMessage = Translations.Attribute.REQUIRED)]

如何使用默认翻译消息创建自定义 RequiredMaxLength 验证。我可以简单地覆盖它并只更改 errorMessage 吗?

我只想写

[MyMaxLength(45)]
[MyRequired]

所需的解决方案:

public class MyRequiredAttribute : RequiredAttribute
{
public override string FormatErrorMessage(string name)
{
return string.Format("Polje {0} je obvezno", name);
}
}

最佳答案

您应该能够从 MaxLengthAttribute 或您正在使用的任何其他属性派生...

public class MyMaxLengthAttribute : MaxLengthAttribute
{
public MyMaxLengthAttribute(int length) : base(length)
{
ErrorMessage = Translations.Attribute.MAX_LENGTH;
}

// other ctors/members as needed
}

关于c# - 如何使用新错误消息创建自定义最大长度和必需验证,逻辑保持不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25259619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com