MVC5中的自定义验证错误消息

时间:2022-11-29 13:53:57

I am quite new to MVC5 and asp.net and I couldn't find the answer, so I would be grateful if someone could tell me how to customize the message after failing the validation. Let's assume I have a code like this:

我是MVC5和asp.net的新手,我找不到答案,所以如果有人能告诉我如何在验证失败后自定义消息,我将不胜感激。我们假设我有一个这样的代码:

    [Required]
    [MaxLength(11),MinLength(11)]
    [RegularExpression("^[0-9]+$")]

    public string Pesel { get; set; }

After using any other signs than digits I got a message like this: The field Pesel must match the regular expression '^[0-9]+$'

在使用除数字之外的任何其他符号后,我得到如下消息:字段Pesel必须匹配正则表达式'^ [0-9] + $'

How can I change this message?

我该如何更改此消息?

3 个解决方案

#1


20  

All validation attributes within System.ComponentModel.DataAnnotations have an ErrorMessage property that you can set:

System.ComponentModel.DataAnnotations中的所有验证属性都具有可以设置的ErrorMessage属性:

[Required(ErrorMessage = "Foo")]
[MinLength(11, ErrorMessage = "Foo"), MaxLength(11, ErrorMessage = "Foo")]
[RegularExpression("^[0-9]+$", ErrorMessage = "Foo")]

Additionally, you can still use the field name / display name for the property within the error message. This is done through a String Format setup. The following example will render an error message of "You forgot MyPropertyName".

此外,您仍然可以在错误消息中使用属性的字段名称/显示名称。这是通过字符串格式设置完成的。以下示例将呈现“您忘记MyPropertyName”的错误消息。

[Required(ErrorMessage = "You forgot {0}")]
public string MyPropertyName { get; set; }

This also respects the DisplayAttribute. Since MyPropertyName isn't a very user-friendly name, the example below will render an error message of "You forgot My Property".

这也尊重DisplayAttribute。由于MyPropertyName不是一个非常用户友好的名称,下面的示例将呈现“您忘记了我的属性”的错误消息。

[Display(Name = "My Property")]
[Required(ErrorMessage = "You forgot {0}")]
public string MyPropertyName { get; set; }

And finally, you can use additional String Format values to render the values and options that are used in the more complex validation attributes, such as the MinLengthAttribute that you are using. This last example will render an error message of "The minimum length for My Property is 11":

最后,您可以使用其他字符串格式值来呈现更复杂的验证属性中使用的值和选项,例如您正在使用的MinLengthAttribute。最后一个示例将呈现错误消息“My Property的最小长度为11”:

[Display(Name = "My Property")]
[MinLength(11, ErrorMessage = "The minimum length for {0} is {1}")]
public string MyPropertyName { get; set; }

#2


1  

The RegularExpression attribute has an ErrorMessage argument.

RegularExpression属性具有ErrorMessage参数。

[RegularExpression("^[0-9]+$","Error Message")]

#3


0  

Giving appropriate error message is good practice because some time we set multiple validation on same property ... to Identify different validation for same property we can assign error message attribute "(ErrorMessage = 'message will be here')"
e.g.,:

提供适当的错误消息是很好的做法,因为有时我们在同一属性上设置多个验证...为了识别相同属性的不同验证,我们可以分配错误消息属性“(ErrorMessage ='message will here here')”例如:

[Required(ErrorMessage = "Username Must not be blank")]
[MinLength(8, ErrorMessage = "Too short Username"), MaxLength(20, ErrorMessage = "UserName must be less than 20")]
[RegularExpression("^[0-9][a-z][A-Z]+$", ErrorMessage = "Username must be combination of number,letter(Capital and Small)")]
public string UserName { get; set; }

#1


20  

All validation attributes within System.ComponentModel.DataAnnotations have an ErrorMessage property that you can set:

System.ComponentModel.DataAnnotations中的所有验证属性都具有可以设置的ErrorMessage属性:

[Required(ErrorMessage = "Foo")]
[MinLength(11, ErrorMessage = "Foo"), MaxLength(11, ErrorMessage = "Foo")]
[RegularExpression("^[0-9]+$", ErrorMessage = "Foo")]

Additionally, you can still use the field name / display name for the property within the error message. This is done through a String Format setup. The following example will render an error message of "You forgot MyPropertyName".

此外,您仍然可以在错误消息中使用属性的字段名称/显示名称。这是通过字符串格式设置完成的。以下示例将呈现“您忘记MyPropertyName”的错误消息。

[Required(ErrorMessage = "You forgot {0}")]
public string MyPropertyName { get; set; }

This also respects the DisplayAttribute. Since MyPropertyName isn't a very user-friendly name, the example below will render an error message of "You forgot My Property".

这也尊重DisplayAttribute。由于MyPropertyName不是一个非常用户友好的名称,下面的示例将呈现“您忘记了我的属性”的错误消息。

[Display(Name = "My Property")]
[Required(ErrorMessage = "You forgot {0}")]
public string MyPropertyName { get; set; }

And finally, you can use additional String Format values to render the values and options that are used in the more complex validation attributes, such as the MinLengthAttribute that you are using. This last example will render an error message of "The minimum length for My Property is 11":

最后,您可以使用其他字符串格式值来呈现更复杂的验证属性中使用的值和选项,例如您正在使用的MinLengthAttribute。最后一个示例将呈现错误消息“My Property的最小长度为11”:

[Display(Name = "My Property")]
[MinLength(11, ErrorMessage = "The minimum length for {0} is {1}")]
public string MyPropertyName { get; set; }

#2


1  

The RegularExpression attribute has an ErrorMessage argument.

RegularExpression属性具有ErrorMessage参数。

[RegularExpression("^[0-9]+$","Error Message")]

#3


0  

Giving appropriate error message is good practice because some time we set multiple validation on same property ... to Identify different validation for same property we can assign error message attribute "(ErrorMessage = 'message will be here')"
e.g.,:

提供适当的错误消息是很好的做法,因为有时我们在同一属性上设置多个验证...为了识别相同属性的不同验证,我们可以分配错误消息属性“(ErrorMessage ='message will here here')”例如:

[Required(ErrorMessage = "Username Must not be blank")]
[MinLength(8, ErrorMessage = "Too short Username"), MaxLength(20, ErrorMessage = "UserName must be less than 20")]
[RegularExpression("^[0-9][a-z][A-Z]+$", ErrorMessage = "Username must be combination of number,letter(Capital and Small)")]
public string UserName { get; set; }