title: Data Annotations Custom Validator not working (as I'd expect) in MVC2
body: <p>Before I start ... I can't easily migrate the project to MVC3. So.</p>
  
  <p>The problem I'm having is that I've defined a custom validator attribute to check the max AND min length of a string property, <em>StringLengthInRangeAttribute</em>.</p>
  
  <p>When the Controller calls <em>ModelState.IsValid</em>, on a list of <em>Passengers</em>  only the validation of a Date property is throwing invalid, when nothing has been supplied. I guess that means my problem is not with the custom validator but all validation?</p>
  
  <h3>Update (additional info for clarity):</h3>
  
  <p>I have two symptoms of this problem :<br>
  1.The Required validator on the <em>strings</em> doesn't fire when they are <em>empty</em><br>
  and<br>
  2.My custom validator never gets called (a breakpoint I set never gets hit).</p>
  
  <h3>Model:</h3>
  
  <pre><code> public class Passenger
   {
      [Required(ErrorMessageResourceType = typeof(Resources.Messages.Passenger),
          ErrorMessageResourceName = "RequireNumber")]
      public int Number { get; set; }
  
      [Required(ErrorMessageResourceType = typeof(Resources.Messages.Passenger),
          ErrorMessageResourceName = "RequireSurname")]
      [StringLengthInRange(MinLength = 2, MaxLength = 30, ErrorMessageResourceType = typeof(Resources.Messages.Passenger),
          ErrorMessageResourceName = "MaxLengthSurname")]
      public string Surname { get; set; }
   } 
  </code></pre>
  
  <h3>Custom Validator:</h3>
  
  <pre><code>public class StringLengthInRangeAttribute:ValidationAttribute
  {
      public int MinLength { get; set; }
      public int MaxLength { get; set; }
  
      public override bool IsValid(object value)
      {
          if (((string)value).Length &lt; MinLength)
          {
              return false;
          }
  
          if (((string)value).Length &gt; MaxLength)
          {
              return false;
          }
  
          return true;
      }
  }
  </code></pre>
  
  <h3>Controller Action:</h3>
  
  <pre><code> public ViewResult TailorHoliday(List&lt;SearchAndBook.Models.ViewModels.Passenger&gt; passengers, 
          int leadPassengerIndex)
      {
          if(!ModelState.IsValid)
          {
              return View("PassengerDetails", GetBookingState(_currentSession));
          }
      //...
      return View();
     }
  </code></pre>
  
  <p>Any advice appreciated. This is the first time I've used Data Annotations, so I'm quite prepared to feel stupid for missing something!</p>