Guest User

Untitled

a guest
Jul 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. public sealed class IsDateAfter : ValidationAttribute, IClientValidatable
  2. {
  3. private readonly string testedPropertyName;
  4. private readonly bool allowEqualDates;
  5.  
  6. public IsDateAfter(string testedPropertyName, bool allowEqualDates = false)
  7. {
  8. this.testedPropertyName = testedPropertyName;
  9. this.allowEqualDates = allowEqualDates;
  10. }
  11.  
  12. protected override ValidationResult IsValid(object value, ValidationContext validationContext)
  13. {
  14. var propertyTestedInfo = validationContext.ObjectType.GetProperty(this.testedPropertyName);
  15. if (propertyTestedInfo == null)
  16. {
  17. return new ValidationResult(string.Format("unknown property {0}", this.testedPropertyName));
  18. }
  19.  
  20. var propertyTestedValue = propertyTestedInfo.GetValue(validationContext.ObjectInstance, null);
  21.  
  22. if (value == null || !(value is DateTime))
  23. {
  24. return ValidationResult.Success;
  25. }
  26.  
  27. if (propertyTestedValue == null || !(propertyTestedValue is DateTime))
  28. {
  29. return ValidationResult.Success;
  30. }
  31.  
  32. // Compare values
  33. if ((DateTime)value >= (DateTime)propertyTestedValue)
  34. {
  35. if (this.allowEqualDates)
  36. {
  37. return ValidationResult.Success;
  38. }
  39. if ((DateTime)value > (DateTime)propertyTestedValue)
  40. {
  41. return ValidationResult.Success;
  42. }
  43. }
  44.  
  45. return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
  46. }
  47.  
  48. public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
  49. {
  50. var rule = new ModelClientValidationRule
  51. {
  52. ErrorMessage = this.ErrorMessageString,
  53. ValidationType = "isdateafter"
  54. };
  55. rule.ValidationParameters["propertytested"] = this.testedPropertyName;
  56. rule.ValidationParameters["allowequaldates"] = this.allowEqualDates;
  57. yield return rule;
  58. }
  59.  
  60. public virtual DateTime StartDate { get; set; }
  61.  
  62. [IsDateAfter("StartDate", true, ErrorMessage="End date needs to be after start date")]
  63. public virtual DateTime EndDate { get; set; }
  64.  
  65. $.validator.unobtrusive.adapters.add(
  66. 'isdateafter', ['propertytested', 'allowequaldates'], function (options) {
  67. options.rules['isdateafter'] = options.params;
  68. options.messages['isdateafter'] = options.message;
  69. });
  70. $.validator.addMethod("isdateafter", function(value, element, params) {
  71. alert(params.propertytested);
  72. var startdatevalue = $('input[name="' + params.propertytested + '"]').val();
  73. if (!value || !startdatevalue) return true;
  74. return (params.allowequaldates) ? Date.parse(startdatevalue) <= Date.parse(value) : Date.parse(startdatevalue) < Date.parse(value);
  75. }, '');
  76.  
  77. public class TrainingDateEditViewModel
  78. {
  79. #region Properties
  80.  
  81. /// <summary>
  82. /// Gets or sets CalendarEntry.
  83. /// </summary>
  84. public CalendarEntry CalendarEntry { get; set; }
  85. ....
  86.  
  87. <input type="text" value="" name="CalendarEntry.EndDate" id="CalendarEntry_EndDate" data-val-isdateafter-propertytested="StartDate" data-val-isdateafter-allowequaldates="True" data-val-isdateafter="End date needs to be after start date" data-val="true">
  88.  
  89. data-val-isdateafter-propertytested="StartDate" and IT SHOULD BE: "CalendarEntry.StartDate".
  90.  
  91. $.validator.addMethod("isdateafter", function(value, element, params) {
  92. var parts = element.name.split(".");
  93. var prefix = "";
  94. if (parts.length > 1)
  95. prefix = parts[0] + ".";
  96. var startdatevalue = $('input[name="' + prefix + params.propertytested + '"]').val();
  97. if (!value || !startdatevalue)
  98. return true;
  99. return (params.allowequaldates) ? Date.parse(startdatevalue) <= Date.parse(value) :
  100. Date.parse(startdatevalue) < Date.parse(value);
  101. });
Add Comment
Please, Sign In to add comment