title: MVC DataValidationAttribute not working
body: <p>Like most out-of-the-box .Net MVC validation attributes, I'm expecting the custom validation below to alert the user to an error before they submit the page. For example, if I set up a field to be a max length of 9 characters and they input 12, a message will appear correcting the user once they go over the limit. </p>
  
  <p>I've created a custom validation attribute to compare a start and end date range, and I'd like it to behave the same way (alert the user before submitting the form). However, the only error message that I can get to display is the standard "This is not a proper date format" message.  I cannot get my custom error messages to display and, after setting a breakpoint on the <code>IsValid</code> method, it looks like this code isn't even being run. </p>
  
  <p><strong>Custom Validation Attribute</strong></p>
  
  <pre><code>public class DateValidationAttribute : ValidationAttribute
  {
      private DateTime? _rangeStart;
      private DateTime? _rangeEnd;
      private string _defaultErrorMessage;
      private string _propertyNameToCompare;
      private ValidationType _validationType;
  
      public enum ValidationType
      {
          StartCompareToEnd,
          EndCompareToStart
      }
  
      public DateValidationAttribute(ValidationType type, string message, string compareWith ="")
      {
          _validationType = type;
          _propertyNameToCompare = compareWith;
          _defaultErrorMessage = message;
      }
  
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
          var basePropertyInfo = validationContext.ObjectType.GetProperty(_propertyNameToCompare);
          Type classType = typeof(PENSGC_Generator.Models.FormViewModel);
          PropertyInfo methodInfo = classType.GetProperty(_propertyNameToCompare);
          DisplayAttribute displayAttribute =
              (DisplayAttribute)Attribute.GetCustomAttribute(methodInfo, typeof(DisplayAttribute));
  
          switch (_validationType)
          {
              case ValidationType.EndCompareToStart:
                  _rangeEnd = (DateTime)basePropertyInfo.GetValue(validationContext.ObjectInstance, null);
                  _rangeStart = (DateTime) value;
                  break;
              case ValidationType.StartCompareToEnd:
                  _rangeStart = (DateTime)basePropertyInfo.GetValue(validationContext.ObjectInstance, null);
                  _rangeEnd = (DateTime) value;
                  break;
          }
  
          string message = string.Format(_defaultErrorMessage, validationContext.DisplayName, displayAttribute.Name);
  
          if (!_rangeStart.HasValue)
          {
              return new ValidationResult(message);
          }
          else if (_rangeEnd.HasValue)
          {
              if (_rangeStart &gt;= _rangeEnd)
              {
                  return new ValidationResult(message);
              }
          }
  
          return base.IsValid(value, validationContext);
      }
  }
  </code></pre>
  
  <p><strong>Model Properties</strong></p>
  
  <pre><code>    [DisplayName("Range Start")]
      [DateValidation(DateValidationAttribute.ValidationType.StartCompareToEnd,  "Start Date must be less than End Date", compareWith: "SearchCreatedRangeEnd")]
      public Nullable&lt;System.DateTime&gt; SearchCreatedRangeStart { get; set; }
      [DisplayName("Range End")]
      [DateValidation(DateValidationAttribute.ValidationType.EndCompareToStart,  "End Date must be greater than Start Date", compareWith: "SearchCreatedRangeStart")]
      public Nullable&lt;System.DateTime&gt; SearchCreatedRangeEnd { get; set; }
  </code></pre>
  
  <p><strong>View/Razor</strong></p>
  
  <pre><code>        &lt;tr&gt;
              &lt;td&gt;
                  &lt;div class="editor-label"&gt;
                      @Html.LabelFor(model =&gt; model.SearchCreatedRangeStart)
                  &lt;/div&gt;
                  &lt;div class="editor-field"&gt;
                      @Html.TextBoxFor(model =&gt; model.SearchCreatedRangeStart, new { style = "width:80px;" })
                      @Html.ValidationMessageFor(model =&gt; model.SearchCreatedRangeStart)
                  &lt;/div&gt;
              &lt;/td&gt;
  
          &lt;/tr&gt;
          &lt;tr&gt;
               &lt;td&gt;
                  &lt;div class="editor-label"&gt;
                      @Html.LabelFor(model =&gt; model.SearchCreatedRangeEnd)
                  &lt;/div&gt;
                  &lt;div class="editor-field"&gt;
                      @Html.TextBoxFor(model =&gt; model.SearchCreatedRangeEnd, new { style = "width:80px;" })
                      @Html.ValidationMessageFor(model =&gt; model.SearchCreatedRangeEnd)
                  &lt;/div&gt;
              &lt;/td&gt; 
          &lt;/tr&gt;
  </code></pre>
  
  <p>Any ideas? First time using custom attributes, would love any words of wisdom. </p>