Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
socketwiz opened this issue Jun 3, 2011 · 6 comments
Closed

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

socketwiz opened this issue Jun 3, 2011 · 6 comments

Comments

@socketwiz
Copy link

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants