The question here is similar, but I have no inheritance of domain objects. My fields and validation tags are in the following order, but MustBe18 errors and a mandatory error are the only ones that print. I have several other fields in this model with much more validation, but the order of the ValidationAttribute in the code does not seem important. Jfar's answer in a related post seems to suggest that an assistant can be built, but how? How can order be controlled?
[Required(ErrorMessage = "This field is required")] [DisplayName("Date of Birth")] [MustBeValidDate(ErrorMessage = "Must be a valid date")] [MustBe18(ErrorMessage = "You must be 18 years old")] [MustNotBeOver100(ErrorMessage = "This caller is too old")] public string dob { get; set; }
MustBe18: ValidationAttribute (Overloaded IsValid Method)
try { DateTime dob = new DateTime(DateTime.Now.AddYears(-18).Year, DateTime.Now.Month, DateTime.Now.Day); return DateTime.Compare(DateTime.Parse(value.ToString()), dob) <= 0; } catch { return false; }
source share