title: Best date validation in Rails 4?
body: <p>What is the best way to validate a date in Ruby on Rails? I need to make sure the "birthday" is a date, is less than 125 years ago and is not in the future (today is ok).</p>
  
  <p>I have tried three methods:</p>
  
  <p>1) date_validator gem</p>
  
  <p>I used the following code (after installing the gem):</p>
  
  <pre><code>validates :birthday, 
  date: {
    after: Proc.new {Time.now - 125.years}, message: :after,
    before: Proc.new {Time.now}, message: :cannot_be_in_the_future
    }
  </code></pre>
  
  <p>This worked <strong>except</strong> that I could set the date to the number 12 and pass validation.</p>
  
  <p>2) Checking if the date is in a range of dates, in a custom validation method, like so:</p>
  
  <pre><code>from = 125.years.ago.to_date
  to = Time.now.to_date
  unless (from..to).include? birthday
    errors.add(:birthday, :custom_error_msg)
  end
  </code></pre>
  
  <p>This worked well and passed all my tests, <strong>but</strong> the drawback is that you only get one error message. I would have liked separate error messages for the case when the date is in the future, when it is too long ago and when the input is not a date.</p>
  
  <p>3) Multiple checks in a custom validation method, like so:</p>
  
  <pre><code>begin
    birthday.to_date
  rescue
    errors.add(:birthday, "must be a date")
  else
    if birthday &gt; Time.now
      errors.add(:birtday, "cannot be in the future")
    elsif birthday &lt; Time.now - 125.years
      errors.add(:birthday, "cannot be over 125 years ago")
    end
  end
  </code></pre>
  
  <p>This also passes all my test, and I get different messages as explained above.</p>
  
  <p>So I am wondering, is this the best method? Can it be improved at all (except that the error message text needs work)?</p>
  
  <p>Thanks</p>