Guest User

Untitled

a guest
Nov 29th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. public class DateGreaterThanAttribute : ValidationAttribute
  2. {
  3. private string _startDatePropertyName;
  4.  
  5. public DateGreaterThanAttribute(string startDatePropertyName)
  6. {
  7. _startDatePropertyName = startDatePropertyName;
  8. }
  9.  
  10. protected override ValidationResult IsValid(object value, ValidationContext validationContext)
  11. {
  12. var propertyInfo = validationContext.ObjectType.GetProperty(_startDatePropertyName);
  13. if (propertyInfo == null)
  14. {
  15. return new ValidationResult(string.Format("Unknown property {0}", _startDatePropertyName));
  16. }
  17. var propertyValue = propertyInfo.GetValue(validationContext.ObjectInstance, null);
  18. if ((DateTime)value > (DateTime)propertyValue)
  19. {
  20. return ValidationResult.Success;
  21. }
  22. else
  23. {
  24. var startDateDisplayName = propertyInfo
  25. .GetCustomAttributes(typeof(DisplayNameAttribute), true)
  26. .Cast<DisplayNameAttribute>()
  27. .Single()
  28. .DisplayName;
  29.  
  30. return new ValidationResult(validationContext.DisplayName + " must be later than " + startDateDisplayName + ".");
  31. }
  32. }
  33. }
  34.  
  35. public class AddTranscriptViewModel : IValidatableObject
  36. {
  37. ...
  38.  
  39. [DisplayName("Class Start"), Required]
  40. [DataType(DataType.Date)]
  41. [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
  42. [RegularExpression(@"^(1[012]|0?[1-9])[/]([12][0-9]|3[01]|0?[1-9])[/](19|20)dd.*", ErrorMessage = "Date out of range.")]
  43. public DateTime? ClassStart { get; set; }
  44.  
  45. [DisplayName("Class End"), Required]
  46. [DataType(DataType.Date)]
  47. [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
  48. [RegularExpression(@"^(1[012]|0?[1-9])[/]([12][0-9]|3[01]|0?[1-9])[/](19|20)dd.*", ErrorMessage = "Date out of range.")]
  49. [DateGreaterThan("ClassStart")]
  50. public DateTime? ClassEnd { get; set; }
  51.  
  52. ...
  53. }
  54.  
  55. @using (Html.BeginForm("AddManualTranscript", "StudentManagement", FormMethod.Post, new { id = "studentManagementForm", @class = "container form-horizontal" }))
  56. {
  57. ...
  58. <div class="col-md-4" id="divUpdateStudent">@Html.Button("Save Transcript Information", "verify()", false, "button")</div>
  59. ...
  60. <div class="col-md-2">
  61. <div id="divClassStart">
  62. <div>@Html.LabelFor(d => d.ClassStart, new { @class = "control-label" })</div>
  63. <div>@Html.EditorFor(d => d.ClassStart, new { @class = "form-control" }) </div>
  64. <div>@Html.ValidationMessageFor(d => d.ClassStart)</div>
  65. </div>
  66. </div>
  67.  
  68. <div class="col-md-2">
  69. <div id="divClassEnd">
  70. <div>@Html.LabelFor(d => d.ClassEnd, new { @class = "control-label" })</div>
  71. <div>@Html.EditorFor(d => d.ClassEnd, new { @class = "form-control" }) </div>
  72. <div>@Html.ValidationMessageFor(d => d.ClassEnd)</div>
  73. </div>
  74. </div>
  75. ...
  76. }
  77.  
  78. <script type="text/javascript">
  79. ...
  80. function verify() {
  81.  
  82. if ($("#StudentGrades").data("tGrid").total == 0) {
  83. alert("Please enter at least one Functional Area for the transcript grades.");
  84. }
  85. else {
  86. $('#studentManagementForm').trigger(jQuery.Event("submit"));
  87. }
  88. }
  89. ...
  90. </script>
  91.  
  92. DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DateGreaterThanAttribute), typeof(DataAnnotationsModelValidator));
Add Comment
Please, Sign In to add comment