Links
Validates date time
Categories
Validates date time
= validatesdatetime
If you find this plugin useful, please consider a donation to show your support!
http://www.paypal.com/cgi-bin/webscr?cmd=_send-money
Email address: jonathan.viney@gmail.com
== Instructions
This plugin adds the ability to do stricter date and time checking with ActiveRecord.
The validators can be used to parse strings into Date and Time objects as well as restrict an attribute based on other dates or times.
class Person < ActiveRecord::Base validatesdate :dateof_birth validatestime :timeof_birth validatesdatetime :dateandtime_of_birth end
Use :allow_nil to allow the value to be blank.
class Person < ActiveRecord::Base validatesdate :dateofbirth, :allownil => true end
== Installation
script/plugin install http://svn.viney.net.nz/things/rails/plugins/validatesdatetime
== Supported formats
The default for the plugin is to expect dates in day/month/year format. If you are in the US, you will want to change the default to month/day/year by placing the following in config/environment.rb
ActiveRecord::Validations::DateTime.usdateformat = true
Date format examples:
- 2006-01-01
- 1 Jan 06
- 1 Jan 2006
- 10/1/06
- 1/1/2006
Time format examples:
- 1pm
- 10:11
- 12:30pm
- 8am
Datetime format examples:
- 1 Jan 2006 2pm
- 31/1/06 8:30am
== Examples
If an attribute value can not be parsed correctly, an error is added:
p = Person.new p.date_of_birth = "1 Jan 2006" p.time_of_birth = "5am" p.save # true
p.date_of_birth = "30 Feb 2006" p.save # false, 30 feb is invalid for obvious reasons
p.date_of_birth = "java is better than ruby" p.save # false
In the final example, as I'm sure you are aware, the record failed to save not only because "java is better than ruby" is an invalid date, but more importantly, because the statement is blatantly false. ;)
=== Restricting date and time ranges
Using the :before and :after options you can restrict a date or time value based on other attribute values and predefined values. You can pass as many value to :before or :after as you like.
class Person validatesdate :dateofbirth, :before => [:dateofdeath, Proc.new { 1.day.fromnow_to_date}], :after => '1 Jan 1900' validatesdate :dateofdeath, :before => Proc.new { 1.day.fromnow.to_date } end
p = Person.new p.date_of_birth = '1800-01-01' p.save # false p.errors[:date_of_birth] # must be after 1 Jan 1900
p.date_of_death = Date.new(2010, 1, 1) p.save # false p.errors[:date_of_death] # must be before
p.date_of_birth = '1960-03-02' p.date_of_death = '2003-06-07' p.save # true
You can customise the error messages for dates or times that fall outside the required range. The boundary date will be substituted in for %s. Eg:
class Person validatesdate :dateofbirth, :after => Date.new(1900, 1, 1), :before => Proc.new { 1.day.fromnow.todate }, :beforemessage => 'Ensure it is before %s', :after_message => 'Ensure it is before %s' end
Suggestions, comments, problems are all welcome. You'll find me at jonathan.viney@gmail.com
Vitals
| Repository | http://svn.viney.net.nz/things/rails/plugins/validates_date_time/ |
|---|---|
| License | Rails' (MIT) |
| Tags |
cpf date dssd fjhf hjgfjf time validates works
|
| Rating | (38 votes) |
| Created | 14 April 2006 |
Comments
-
I couldn't get this to work in a consistent way. I don't think that it handles strftime properly or takes into account variations in database timestamp format.
-
I wouldn't recommend this plugin. The idea is nice, but it attempts to do its own date and/or time parsing. Why not just rely on the current string to date conversion in rails? (which is handled by the DB driver). The idea is good, but for some reason, the author went overboard. I want to reduce the amount of ways I can format or parse a date, not add to the confusion.
-
Very Nice. I have used this plugin to do no fuss date validation in the european format and found it very useful. It is flexibe and will allow a good sensible range of formats.
Thanks -
Worked fine for me, very useful little plugin!
-
I had some issues getting this to work properly; it appears to intercept method calls going to DateTime, so I ended up seeing errors like this:
NoMethodError: undefined method `now' for ActiveRecord::Validations::DateTime:Module
which is pretty inconvenient.
So I added a method_missing around like 155 of validates_date_time.rb like this:
def self.method_missing(m, args) DateTime.send(m, args) end
but I think the more optimal solution would probably be to have this plugin interfere less with DateTime if possible. I haven't got the time to investigate much further at the moment, but just this small fix does make all my tests pass. Fingers crossed. :-)
-
This plugin doesn't work well with Rails 2.2. I ended up using validates_timeliness instead.
If you want the details: http://blog.costan.us/2009/01/date-time-and-datetime-validation-in.html
-
I had some errors when using the method
validates_date :date, :before :another_date
When an error is raised, I got this : wrong number of arguments (3 for 2) vendor/plugins/validates_date_time/lib/validates_date_time.rb:117:in `add'
I fixed it changing line
record.errors.add(attr_name, :before, :value => r, :default => options[:before_message] % r)
into
record.errors.add(attr_name, :default => options[:before_message] % r)
(I'm using Rails 2.1.1)
-
I think this plugin is a bit of a hack. I don't want to use it. To tell the truth, I don't like the way Rails handles date_select with invalid dates in the first place.
I certainly do not want to trust this plugin with Rails 2.3, since it was not written for it - bring on Rails 3/Merb 2 with a standard plugin API!
I think I'm just going to do the validation in the controller...
-
I just want to say that your plugin is changing hours to days, min to hours in the time_select function. It took me an entire workday to figure out that it was your plugin that fucked up all my data. Now im back to sqauare one.
-
just in case someone needs to use US format, teh right code to add to environment.rb is
ActiveRecord::Validations::DateTime.us_date_format = true
HTH
-
is it possible to have a conditional check ? I wrote (check again record status field )
works fine without the :if... how should I write it ?
thanks again

