4

我为出生日期创建了验证属性,如下所示:

public class DateRequired : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            DateTime date = Convert.ToDateTime(value);
            return date != DateTime.MinValue;
        }
    }

    public class DateGraterThanEighteen : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            DateTime date = Convert.ToDateTime(value);
            long ticks = DateTime.Now.Ticks - date.Ticks;
            int years = new DateTime(ticks).Year;
            return years >= 18;
        }
    }

I applied attributes like this

        [DateGraterThanEighteen(ErrorMessage="You must be at least 18")]
        [DateRequired(ErrorMessage = "Date of birth is required")]
        public DateTime DateOfBirth { get; set; }

无论我应用它们的顺序如何,DateGraterThanEighteen 都会首先执行。我怎样才能让 DateRequired 先执行?

4

1 回答 1

0

您可以将订单应用于过滤器。不确定是否可以使用验证属性尝试

[DateGraterThanEighteen(ErrorMessage="You must be at least 18"), Order=1]
[DateRequired(ErrorMessage = "Date of birth is required"), Order=2]
public DateTime DateOfBirth { get; set; }

仅供参考,你拼错了更大 :)

更新:没关系,不能这样做。你需要创建一个ModelValidationProvider

于 2012-07-14T01:45:18.097 回答