Dave Silvester
2005-Sep-29 17:32 UTC
Date validation again - code that consolidates dates?
Where in Rails would I find the code that deals with consolidating the various elements of a group of date/time selection boxes back into one datetime value? For example, from a datetime_select or select_date. I need to alter how it works to make it throw up an error on invalid dates (31st September, 30th February etc.) and although I now have a neat way of doing this for one case where I''m handling dates myself (not saving them directly into the database), I don''t quite know where to tackle this problem for the stuff that ActiveRecord takes care of. I really need my dates to be checked to make sure they are valid. Just out of interest, is there a technical reason why RoR will roll over invalid dates to the next valid date, so September 31st becomes October 1st? It would be nice if there were a "strict" option to raise an error when an invalid date was entered, and I''m wondering if I might be able to write that and submit it as a patch? If not, I certainly need to set it up for my own uses, so, if someone could point me in the right direction for where to find those functions, and possibly about how to side-step around them or override their workings, that''d be great. I''ve tried grepping in my /usr/lib/ruby/gems/1.8/gems but can''t find many instances of mktime except for some test code... and I''m not sure what else it could be done using? Perhaps it''s not ActiveRecord that handles this, but I thought it would be? Surely I''m not the only person for whom this acceptance of invalid dates causes problems? Even something like a validates_date check would be good - perhaps that''s the easiest answer? However, it would seem that the problem there is that the field is split into separate parts, then automatically consolidated, and only made available for validation after that... as far as I can tell? How do we validate it (in as non-kludgy a way as possible) before it gets that far? Any suggestions? I Googled "rails validate_date" and found this Typo patch, but even that seems to just roll the invalid dates over, rather than complain about them: http://typo.leetsoft.com/trac/attachment/ticket/13/calender.patch By the way, thanks to Tom Davies for his little days_in(year, month) function that solved at least one part of my date validation stuff for me before, and saved me having to write that for myself - good stuff! I just need to take this further and get it working on the ActiveRecord-handled dates. Cheers, ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/
Dave Silvester
2005-Sep-30 20:25 UTC
Re: Date validation again - code that consolidates dates?
On Thursday 29 Sep 2005 18:32, Dave Silvester wrote:> Surely I''m not the only person for whom this acceptance of invalid dates > causes problems?... but perhaps I am after all? Is there a pragmatic and sound reason for accepting and rolling-over invalid dates, rather than raising an error? Are others not bothered for a good reason? What do you tell your customers who want enforcement of valid dates when giving them a Rails app that just swallows them, rolls them over to the next day and doesn''t complain? Honestly, I can''t believe I''m the only one this causes a problem for? Surely others must need proper date validation built in to Rails! Surely? Please would someone enlighten me? :-) ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/
Jeremy Kemper
2005-Sep-30 21:31 UTC
Re: Date validation again - code that consolidates dates?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sep 30, 2005, at 1:25 PM, Dave Silvester wrote:> On Thursday 29 Sep 2005 18:32, Dave Silvester wrote: >> Surely I''m not the only person for whom this acceptance of invalid >> dates >> causes problems? > > ... but perhaps I am after all? > > Is there a pragmatic and sound reason for accepting and rolling- > over invalid > dates, rather than raising an error? Are others not bothered for a > good > reason? What do you tell your customers who want enforcement of > valid dates > when giving them a Rails app that just swallows them, rolls them > over to the > next day and doesn''t complain? > > Honestly, I can''t believe I''m the only one this causes a problem > for? Surely > others must need proper date validation built in to Rails! Surely? > > Please would someone enlighten me? :-)Try using a DATE column instead of TIMESTAMP or DATETIME. DATE maps to Ruby''s Date but the others map to Ruby''s Time. Date raises an exception on invalid dates; Rails catches it and sets the date to nil. Time, however, only raises an exception if a basic range check fails, then tries its damnedest to make sense of the params its given. See the insance search_time_t in time.c -- wow. For correct behavior, we should consider switching from Time to DateTime, at least for parsing. >> require ''time'' => true >> Time.parse "Fri Sep 31 14:28:58 PDT 2005" => Sat Oct 01 14:28:58 PDT 2005 >> require ''date'' => true >> DateTime.parse "Fri Sep 31 14:28:58 PDT 2005" ArgumentError: invalid date from /usr/local/lib/ruby/1.8/date.rb:1173:in `civil'' from /usr/local/lib/ruby/1.8/date.rb:1216:in `new_with_hash'' from /usr/local/lib/ruby/1.8/date.rb:1258:in `parse'' from (irb):4 Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD4DBQFDPa7FAQHALep9HFYRAjSRAJj4ITzuZ7JgBaboRmy5b1IxPZOoAKDZKy4r h/oFvZ1U71hgVUkOWFCZQQ==OQA0 -----END PGP SIGNATURE-----