Comments (6)
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 :)
from date_validator.
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'
}
from date_validator.
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?
from date_validator.
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?
from date_validator.
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
from date_validator.
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.
from date_validator.
Related Issues (20)
- Need to check time is a valid time HOT 5
- Wrong type of error in different cases HOT 2
- time range HOT 1
- Update activemodel version dependency HOT 1
- Add way to check is a string is a valid date HOT 5
- Complain of missing translation HOT 7
- Fail to detect errors when invalid strings are provided HOT 1
- Not working in certain time zones HOT 3
- Differentiating between `blank` and `not_a_date` HOT 4
- I18n conflicts with Activemodel I18n HOT 4
- v0.9.0 introduced a side effect about I18n.locale_available? HOT 5
- URL out of date in repository description HOT 1
- Rails 5 HOT 2
- Not really ORM-Agnostic HOT 2
- Is this gem still maintained ? HOT 2
- Add a changelog HOT 1
- Skip { after: :other_attribute } validation if other_attribute is nil HOT 1
- Proc in error details
- Validation doesn't work if the value is represented as a string
- Is there any way that I can make sure that ranges are exclusive?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from date_validator.