Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DateGreaterThanAttribute : ValidationAttribute
- {
- private string _startDatePropertyName;
- public DateGreaterThanAttribute(string startDatePropertyName)
- {
- _startDatePropertyName = startDatePropertyName;
- }
- protected override ValidationResult IsValid(object value, ValidationContext validationContext)
- {
- var propertyInfo = validationContext.ObjectType.GetProperty(_startDatePropertyName);
- if (propertyInfo == null)
- {
- return new ValidationResult(string.Format("Unknown property {0}", _startDatePropertyName));
- }
- var propertyValue = propertyInfo.GetValue(validationContext.ObjectInstance, null);
- if ((DateTime)value > (DateTime)propertyValue)
- {
- return ValidationResult.Success;
- }
- else
- {
- var startDateDisplayName = propertyInfo
- .GetCustomAttributes(typeof(DisplayNameAttribute), true)
- .Cast<DisplayNameAttribute>()
- .Single()
- .DisplayName;
- return new ValidationResult(validationContext.DisplayName + " must be later than " + startDateDisplayName + ".");
- }
- }
- }
- public class AddTranscriptViewModel : IValidatableObject
- {
- ...
- [DisplayName("Class Start"), Required]
- [DataType(DataType.Date)]
- [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
- [RegularExpression(@"^(1[012]|0?[1-9])[/]([12][0-9]|3[01]|0?[1-9])[/](19|20)dd.*", ErrorMessage = "Date out of range.")]
- public DateTime? ClassStart { get; set; }
- [DisplayName("Class End"), Required]
- [DataType(DataType.Date)]
- [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
- [RegularExpression(@"^(1[012]|0?[1-9])[/]([12][0-9]|3[01]|0?[1-9])[/](19|20)dd.*", ErrorMessage = "Date out of range.")]
- [DateGreaterThan("ClassStart")]
- public DateTime? ClassEnd { get; set; }
- ...
- }
- @using (Html.BeginForm("AddManualTranscript", "StudentManagement", FormMethod.Post, new { id = "studentManagementForm", @class = "container form-horizontal" }))
- {
- ...
- <div class="col-md-4" id="divUpdateStudent">@Html.Button("Save Transcript Information", "verify()", false, "button")</div>
- ...
- <div class="col-md-2">
- <div id="divClassStart">
- <div>@Html.LabelFor(d => d.ClassStart, new { @class = "control-label" })</div>
- <div>@Html.EditorFor(d => d.ClassStart, new { @class = "form-control" }) </div>
- <div>@Html.ValidationMessageFor(d => d.ClassStart)</div>
- </div>
- </div>
- <div class="col-md-2">
- <div id="divClassEnd">
- <div>@Html.LabelFor(d => d.ClassEnd, new { @class = "control-label" })</div>
- <div>@Html.EditorFor(d => d.ClassEnd, new { @class = "form-control" }) </div>
- <div>@Html.ValidationMessageFor(d => d.ClassEnd)</div>
- </div>
- </div>
- ...
- }
- <script type="text/javascript">
- ...
- function verify() {
- if ($("#StudentGrades").data("tGrid").total == 0) {
- alert("Please enter at least one Functional Area for the transcript grades.");
- }
- else {
- $('#studentManagementForm').trigger(jQuery.Event("submit"));
- }
- }
- ...
- </script>
- DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DateGreaterThanAttribute), typeof(DataAnnotationsModelValidator));
Add Comment
Please, Sign In to add comment