Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class YearCheck : ValidationAttribute
- {
- protected override ValidationResult IsValid(object value, ValidationContext validationContext)
- {
- Car car = (Car)validationContext.ObjectInstance;
- if (car == null)
- throw new ArgumentException("Atribute not applied on Car");
- if(car.Year > DateTime.Now.Year)
- {
- return new ValidationResult(GetErrorMessage(validationContext));
- }
- return ValidationResult.Success;
- }
- private string GetErrorMessage(ValidationContext validationContext)
- {
- if (!string.IsNullOrEmpty(this.ErrorMessage))
- return this.ErrorMessage;
- return "Can't Create car that has not been created yet";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement