title: Date Validation In Rails 3
body: <p>I've got a Rails 3 app (using Mongodb and Mongoid if that makes a difference), and in one of my models I have a field defined as a Date type.</p>
  
  <pre><code>class Participant
    include Mongoid::Document
  
    field :birth_date, :type => Date
  end
  </code></pre>
  
  <p>My controller is using the find_or_initialize_by feature of mongo:</p>
  
  <pre><code>class ParticipantController
  
    def create
      @participant = Participant.find_or_initialize_by(params[:participant])
      if @participant.save
        redirect_to participants_path
      else
        render :new
      end
    end
  end
  </code></pre>
  
  <p>All this boils down to: <strong>how do I do date validation in ActiveModel with Mongoid and Rails 3?</strong> </p>
  
  <p>I want to make sure an input of "blah" as in a textbox does not throw an exception when assigned to the <code>.birth_date</code> field of my model. It should provides a nice validation error message without using the controller to do the validation. </p>
  
  <p>Here's the basic requirements:</p>
  
  <ul>
  <li><p>The view must be a single textbox.
  Nothing else. This is a user
  requirement that we cannot change </p></li>
  <li><p>The validation should be done in the 
  model, not in the controller or the<br>
  view (javascript or whatever)</p></li>
  <li><p>No regex format validations (they don't work right / support locale's etc)</p></li>
  </ul>
  
  <p>The problem is that the value from the textbox is assigned to the <code>.birth_date</code> before validates_format_of and validates_presence_of are run. so...</p>
  
  <p>how do I intercept the assignment of the value, so that I can validate it before it gets assigned? is it possible to do that in the model, using ActiveModel? or does this require me putting code in the controller to do this?</p>