代码之家  ›  专栏  ›  技术社区  ›  ozz

如何在自定义验证属性中为不同的方案设置多个错误消息?

  •  4
  • ozz  · 技术社区  · 14 年前

    我只是在学习自定义验证属性,并尝试编写一个自定义验证工具,它将被放置在类级别,以针对我的模型的多个属性进行验证。

    我可以访问我的模型上的所有属性,并且我希望能够在isvalid重载中检查多个条件,并报告它们,具有如下不同的错误消息(简单示例)。

    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;
            }
    
            if (model.Prop1 == "blah" && model.Prop2 == 1)
            {
                ErrorMessage = "you can't enter blah if prop 2 equals 1";
                return false;
            }
    
            if(model.Prop1 == "blah blah" && model.Prop2 == 2)
            {
                ErrorMessage = "you can't enter blah blah if prop 2 equals 2";
                return false;
            }
    
    
            return true;
        }
    

    但是当我这样做的时候,我在第一次引用错误消息时得到一个异常“不能多次设置属性”。

    现在我可以将自定义属性拆分为多个自定义属性,但希望有一种方法可以在一个属性中完成,否则,我将在每个属性中重复我的“全部捕获”。

    //if this value is set, I don't want to do anything other checks
            if (model.Prop3)
            {
                return true;
            }
    

    我已经搜索过了,但是找不到任何东西,所以如果我遗漏了任何明显的东西,我深表歉意。

    事先谢谢!

    2 回复  |  直到 7 年前
        1
  •  1
  •   waka    7 年前

    有趣的问题!我可以想出两个解决办法。因此,基于您想要的解决方案并不合适,但它们可能有助于重用您的代码。不能创建一个名为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 model) 是您的抽象方法,那么,您可以编写多个自定义属性类来扩展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属性中的错误消息,并在前端处理它(但我将使用第一种方法)。

        2
  •  3
  •   Peter Kerr    11 年前

    在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;
        }
    }