MinAttribute.cs 675 B

123456789101112131415161718192021222324252627
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. namespace GxPress.Common.Validation
  4. {
  5. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
  6. public class MinAttribute : ValidationAttribute
  7. {
  8. private readonly int _min;
  9. public MinAttribute(int min)
  10. {
  11. _min = min;
  12. }
  13. public override bool IsValid(object value)
  14. {
  15. var min = (int)value;
  16. return min >= _min;
  17. }
  18. public override string FormatErrorMessage(string name)
  19. {
  20. return $"{name}必须大于{_min}";
  21. }
  22. }
  23. }