【问题标题】:ASP MVC Data Annotation - How to verify that two model properties are different using CompareAttributeASP MVC 数据注释 - 如何使用 CompareAttribute 验证两个模型属性是否不同
【发布时间】:2014-02-10 18:28:30
【问题描述】:

我经常在我的模型中使用 CompareAttribute 数据注释来验证两个字段是否相等。例如,我们大多数人使用它来比较密码和确认密码字段。

这似乎微不足道,但我想知道如何使用这样的注释来比较两个字段是否不同。例如,我想验证密码与用户名是否不同。

对于更复杂的验证,我知道我必须使用自定义验证器,但我只是想知道是否有内置的东西。

谢谢。

【问题讨论】:

标签: asp.net asp.net-mvc data-annotations


【解决方案1】:

这里有两个选择,创建自己的 ValidationAttribute 继承自 CompareAttribute 或继承自 ValidationAttribute

1) 自定义ValidationAttribute继承自CompareAttribute

public class NotEqualAttribute : CompareAttribute
{
    public string BasePropertyName { get; private set; }
    private const string DefaultErrorMessage = "'{0}' and '{1}' must not be the same.";
    public MyCustomCompareAttribut(string otherProperty)
        : base(otherProperty)
    {
        BasePropertyName = otherProperty;
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(DefaultErrorMessage, name, BasePropertyName);
    }
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var result = base.IsValid(value, validationContext);
        if (result == null)
        {
            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }
        return null;
    }
}

2)自定义ValidationAttribute继承自ValidationAttribute

  public class NotEqualAttribute : ValidationAttribute
    {
        private const string DefaultErrorMessage = "'{0}' and '{1}' must not be the same.";
        public string BasePropertyName { get; private set; }
        public NotEqualAttribute(string basePropertyName)
            : base(DefaultErrorMessage)
        {
            BasePropertyName = basePropertyName;
        }
        public override string FormatErrorMessage(string name)
        {
            return string.Format(DefaultErrorMessage, name, BasePropertyName);
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var property = validationContext.ObjectType.GetProperty(BasePropertyName);
            if (property == null)
            {
                return new ValidationResult(
                    string.Format(
                        CultureInfo.CurrentCulture, "{0} is invalid property", BasePropertyName
                    )
                );
            }
            var otherValue = property.GetValue(validationContext.ObjectInstance, null);
            if (object.Equals(value, otherValue))
            {
                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }
            return null;
        }
    }

然后你可以使用任何一种:

public class YourModelClass
{
    public string PropertyA{ get; set; }

    [NotEqual("PropertyA")]
    public string PropertyB{ get; set; }
}

【讨论】:

  • 这会引发客户端验证错误还是只是在服务器上?
猜你喜欢
  • 1970-01-01
  • 2017-12-16
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多