-
Notifications
You must be signed in to change notification settings - Fork 82
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
Comments
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 :) |
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'
} |
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? |
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? |
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 |
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. |
I have this in my model:
But it doesn't matter what I enter for a date, I don't get any validation error. If I change it to:
And enter a date before today, it works as expected. Any ideas?
The text was updated successfully, but these errors were encountered: