codegram / date_validator

A simple, ORM agnostic, Ruby >=2.2 compatible date validator for Rails, based on ActiveModel.
http://thoughts.codegram.com/date-validation-with-rails-3
MIT License
496 stars 82 forks source link

doesn't seem to work with just :before #16

Closed socketwiz closed 13 years ago

socketwiz commented 13 years ago

I have this in my model:

  validates :date_completed, :date => {
    :before => Time.now, :message => 'must be before today'
  }

But it doesn't matter what I enter for a date, I don't get any validation error. If I change it to:

  validates :date_completed, :date => {
    :after => Time.now, :message => 'must be after today'
  }

And enter a date before today, it works as expected. Any ideas?

oriolgual commented 13 years ago

The problem is that Time.now is evaluated when the class is loaded. You probably want to do this:

  validates :date_completed, :date => {
    :before => Proc.new { Time.now }, :message => 'must be before today'
  }

As noted at the readme :)

socketwiz commented 13 years ago

Yeah, I tried that before posting, but it doesn't work the way I would expect. When using Proc.new, I get a validation error, that says that I need to choose a date before today, even when choosing a date before today. In fact it doesn't matter what day I choose when using Proc.new, today, tomorrow, yesterday, it just keeps telling me to choose a date before today. The date I am passing in is of the format: 2011-06-01, would that be the problem? I tried 06-01-2011 but that didn't seem to matter either. I'm sure its something silly I'm doing wrong, just not sure what :(

None of these seem to work:

validates :date_completed, :date => {
    :before => Time.now, :message => 'must be before today'
  }
validates :date_completed, :date => {
    :before => Date.today, :message => 'must be before today'
  }
validates :date_completed, :date => {
    :before => Proc.new {Time.now}, :message => 'must be before today'
  }
validates :date_completed, :date => {
    :before => Proc.new{Date.today}, :message => 'must be before today'
  }
validates :date_completed, :date => {
    :before_or_equal_to => Proc.new {Time.now}, :message => 'must be before today'
  }
validates :date_completed, :date => {
    :before_or_equal_to => Proc.new{Date.today}, :message => 'must be before today'
  }
oriolgual commented 13 years ago

date_validator doesn't really care about the format, as long as its a Date, Time or DateTime. Are you sure you are passing a date object instead of a string?

socketwiz commented 13 years ago

Are you sure you are passing a date object instead of a string?

Hmm, I am pretty sure it comes through as a string. I'm just passing it through my form:

  <div class="field">
    <strong><label>Date Completed</label></strong>
    <%= f.text_field :completed_on, :class => "inputbox" %>
  </div>

action:

  def create
    @project.save
    respond_with(@project)
  end

I'm going to have to research how to make it a date object. Off the top of my head I'm thinking you have to open up the params array prior to a save or update in the action? Does that sound right? Or maybe there is a date_field that you use in the view?

oriolgual commented 13 years ago

Yes, you should you a date_input or time_input, this way Rails will autoconvert the value to a Date object. Here are some links to help you:

http://guides.rubyonrails.org/form_helpers.html#using-date-and-time-form-helpers https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/date_helper.rb#L138

socketwiz commented 13 years ago

I see, well thank you for your help. I don't think I am going to be able to use this gem because I also want to use the jQuery datepicker: http://jqueryui.com/demos/datepicker/

But thank you for your time.