NumericValueValidator.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using Abp.Extensions;
  3. namespace Abp.Runtime.Validation
  4. {
  5. [Serializable]
  6. [Validator("NUMERIC")]
  7. public class NumericValueValidator : ValueValidatorBase
  8. {
  9. public int MinValue
  10. {
  11. get { return (this["MinValue"] ?? "0").To<int>(); }
  12. set { this["MinValue"] = value; }
  13. }
  14. public int MaxValue
  15. {
  16. get { return (this["MaxValue"] ?? "0").To<int>(); }
  17. set { this["MaxValue"] = value; }
  18. }
  19. public NumericValueValidator()
  20. {
  21. }
  22. public NumericValueValidator(int minValue = int.MinValue, int maxValue = int.MaxValue)
  23. {
  24. MinValue = minValue;
  25. MaxValue = maxValue;
  26. }
  27. public override bool IsValid(object value)
  28. {
  29. if (value == null)
  30. {
  31. return false;
  32. }
  33. if (value is int)
  34. {
  35. return IsValidInternal((int)value);
  36. }
  37. if (value is string)
  38. {
  39. int intValue;
  40. if (int.TryParse(value as string, out intValue))
  41. {
  42. return IsValidInternal(intValue);
  43. }
  44. }
  45. return false;
  46. }
  47. protected virtual bool IsValidInternal(int value)
  48. {
  49. return value.IsBetween(MinValue, MaxValue);
  50. }
  51. }
  52. }