默认的StringLength验证错误消息对我来说太长了,所以我想缩短它。
我知道我可以为每个属性指定ErrorMessage,但我不想在整个地方重复这个:
[StringLength(25, ErrorMessage = "Max length is {1}")]
[DisplayName("Address")]
public virtual string BillAddress1 { get; set; }
基于这篇文章Modify default ErrorMessage for StringLength validation,我将StringLengthAttribute子类化:
public class CustomStringLengthAttribute : StringLengthAttribute
{
public CustomStringLengthAttribute()
: this(20)
{
}
public CustomStringLengthAttribute(int maximumLength)
: base(maximumLength)
{
base.ErrorMessage = "Max length is {1}";
}
}
并改为使用CustomStringLengthAttribute:
[CustomStringLength(25)]
[DisplayName("Address")]
public virtual string BillAddress1 { get; set; }
这编译得很好,我在CustomStringLengthAttribute构造函数中设置了一个断点来验证它是否被命中,但不显眼的验证不再有效 - 无效数据被发送回控制器。
当我不使用我的子类版本时,它工作正常。
此外,这是javascript验证:
if (validation.validateForm($('#addressForm'))) {
....
}
validateForm:
function validateForm(form) {
prepareValidationScripts(form);
var validator = $.data(form[0], 'validator');
return validator.form();
}
function prepareValidationScripts(form) {
if (form.executed)
return;
form.removeData("validator");
$.validator.unobtrusive.parse(document);
form.executed = true;
}
我错过了什么?
感谢。
答案 0 :(得分:1)
尝试使用自定义验证。
CustomStringLengthAttribute.cs
public class CustomStringLengthAttribute : ValidationAttribute, IClientValidatable
{
public int MaximumLength
{
get;
private set;
}
public int MinimumLength
{
get;
set;
}
public CustomStringLengthAttribute(int maximumLength)
: base(new Func<string>(CustomStringLengthAttribute.GetDefaultErrorMessage))
{
this.MaximumLength = maximumLength;
}
private static string GetDefaultErrorMessage()
{
return "Max length {1}";
}
public override string FormatErrorMessage(string name)
{
return string.Format(base.ErrorMessageString, name, this.MaximumLength, this.MinimumLength);
}
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
string text = (string)value;
int maximumLength = this.MaximumLength;
int minimumLength = this.MinimumLength;
if (maximumLength < 0)
{
throw new InvalidOperationException("The maximum length must be a nonnegative integer.");
}
if (minimumLength > maximumLength)
{
throw new InvalidOperationException(string.Format("The maximum value '{0}' must be greater than or equal to the minimum value '{1}'.", maximumLength, minimumLength));
}
int length = text.Length;
return length <= maximumLength && length >= minimumLength;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationParameters.Add("max", MaximumLength);
rule.ValidationParameters.Add("min", MinimumLength);
rule.ValidationType = "customstringlength";
yield return rule;
}
}
在js文件中:
/// <reference path="jquery-1.4.4.js" />
/// <reference path="jquery.validate.js" />
/// <reference path="jquery.validate.unobtrusive.js" />
if ($.validator && $.validator.unobtrusive) {
$.validator.unobtrusive.adapters.addMinMax("customstringlength", "min","max");
$.validator.addMethod("customstringlength", function (value, element, min,max) {
if (value) {
if (value.length<min || value.length>max) {
return false;
}
}
return true;
});
}
模特课:
[CustomStringLength(25)]
[DisplayName("Address")]
public virtual string BillAddress1 { get; set; }