Asp.net mvc 2 如何在自定义验证属性中为不同场景设置多条错误消息?
我刚刚开始处理自定义验证属性,我正在尝试编写一个自定义验证attirbute,它将被放置在类级别,以根据模型的多个属性进行验证 我可以访问我的模型上的所有属性,并且我希望能够检查IsValid重载中的多个条件,并报告这些条件,具有如下不同的错误消息(简单示例) 但当我这样做时,我在第一次引用ErrorMessage时得到一个异常“不能多次设置属性” 现在我可以将我的自定义属性拆分为多个自定义属性,但希望有一种方法可以在一个自定义属性中完成,否则,我将在每个自定义属性中重复我的“全部捕获” 我已经搜索过了,但是找不到任何东西,所以如果我遗漏了任何明显的东西,请道歉Asp.net mvc 2 如何在自定义验证属性中为不同场景设置多条错误消息?,asp.net-mvc-2,Asp.net Mvc 2,我刚刚开始处理自定义验证属性,我正在尝试编写一个自定义验证attirbute,它将被放置在类级别,以根据模型的多个属性进行验证 我可以访问我的模型上的所有属性,并且我希望能够检查IsValid重载中的多个条件,并报告这些条件,具有如下不同的错误消息(简单示例) 但当我这样做时,我在第一次引用ErrorMessage时得到一个异常“不能多次设置属性” 现在我可以将我的自定义属性拆分为多个自定义属性,但希望有一种方法可以在一个自定义属性中完成,否则,我将在每个自定义属性中重复我的“全部捕获” 我已经
提前感谢!有意思的问题!我可以想出两种解决方法。因此,根据您的需要,这不是合适的解决方案,但它们可能有助于重用您的代码。您不能创建一个名为MyCustomAttribute(或其他)的CustomAttribute抽象类,该类通过以下方式重写是有效的:
public override bool IsValid(object value)
{
var model = (MyObject) value;
//if this value is set, I don't want to do anything other checks
if (model.Prop3)
{
return true;
}
CustomValidate(model);
}
CustomValidate(MyObject模型)
是您的抽象方法,您可以编写多个自定义属性类来扩展MyCustomAttribute,并且只需要为特定场景实现验证逻辑
因此,您可以有两个类:
public class BlahCustomAttribute : MyCustomAttribute
{
public override Boolean CustomValidate(MyObject obj)
{
if (model.Prop1 == "blah" && model.Prop2 == 1)
{
ErrorMessage = "you can't enter blah if prop 2 equals 1";
return false;
}
}
}
public class BlahBlahCustomAttribute : MyCustomAttribute
{
public override Boolean CustomValidate(MyObject obj)
{
if (model.Prop1 == "blah" && model.Prop2 == 1)
{
ErrorMessage = "you can't enter blah blah if prop 2 equals 1";
return false;
}
}
}
希望这对你有所帮助——不是你想要的,但它能完成工作,而且很干净
另一种解决方案是用逗号分隔ErrorMessage属性中的错误消息,并在前端处理它(但我会使用第一种方法)。在MVC4中,您可以覆盖IsValid以返回不同的消息作为ValidationResult
public class StrongPasswordAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext context)
{
if (value == null)
return new ValidationResult("Password is required");
var val = value.ToString();
if (!Regex.Match(val, @"^(?=.*[a-z]).{0,}$").Success)
{
return new ValidationResult("Password must contain at least one lower case letter");
}
if (!Regex.Match(val, @"^(?=.*[A-Z]).{0,}$").Success)
{
return new ValidationResult("Password must contain at least one UPPER case letter");
}
if (!Regex.Match(val, @"^(?=.*\d).{0,}$").Success)
{
return new ValidationResult("Password must contain at least one number");
}
return ValidationResult.Success;
}
}
public class BlahCustomAttribute : MyCustomAttribute
{
public override Boolean CustomValidate(MyObject obj)
{
if (model.Prop1 == "blah" && model.Prop2 == 1)
{
ErrorMessage = "you can't enter blah if prop 2 equals 1";
return false;
}
}
}
public class BlahBlahCustomAttribute : MyCustomAttribute
{
public override Boolean CustomValidate(MyObject obj)
{
if (model.Prop1 == "blah" && model.Prop2 == 1)
{
ErrorMessage = "you can't enter blah blah if prop 2 equals 1";
return false;
}
}
}
public class StrongPasswordAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext context)
{
if (value == null)
return new ValidationResult("Password is required");
var val = value.ToString();
if (!Regex.Match(val, @"^(?=.*[a-z]).{0,}$").Success)
{
return new ValidationResult("Password must contain at least one lower case letter");
}
if (!Regex.Match(val, @"^(?=.*[A-Z]).{0,}$").Success)
{
return new ValidationResult("Password must contain at least one UPPER case letter");
}
if (!Regex.Match(val, @"^(?=.*\d).{0,}$").Success)
{
return new ValidationResult("Password must contain at least one number");
}
return ValidationResult.Success;
}
}