Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. public class YearCheck : ValidationAttribute
  2. {
  3. protected override ValidationResult IsValid(object value, ValidationContext validationContext)
  4. {
  5. Car car = (Car)validationContext.ObjectInstance;
  6.  
  7. if (car == null)
  8. throw new ArgumentException("Atribute not applied on Car");
  9.  
  10. if(car.Year > DateTime.Now.Year)
  11. {
  12. return new ValidationResult(GetErrorMessage(validationContext));
  13. }
  14.  
  15. return ValidationResult.Success;
  16. }
  17.  
  18. private string GetErrorMessage(ValidationContext validationContext)
  19. {
  20. if (!string.IsNullOrEmpty(this.ErrorMessage))
  21. return this.ErrorMessage;
  22.  
  23. return "Can't Create car that has not been created yet";
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement