Handling Date Entries
Published: May 10, 2008 (over 9 years ago)
Updated: almost 3 years ago
Separate inputs for a single Date Entry? pop-up Calendars? Enforce one way (i.e. enter in “mm/dd/yyyy” format)? What happened to the end-user perspective? User input parsing and validation is classic computer science and usability fodder, yet we seem to get ever more clever at making it more cumbersome to enter a date than to get a date! (I’ll leave the pun implications to the reader…)
The Rails Way
Separating inputs into month, day, year drop down lists makes the user click three times with mouse (or worse, type, tab, type, tab, type. It looks cool and minimizes programming effort to get a guaranteed valid date entry, but before Rails came along, I haven’t seen this entry style in practice since the days of IBM 3270 terminal applications! At least in those old applications, there was a predictive key parser narrowing your choices as you typed so it was a seemless type and auto-tab to next field. Thus, a user could enter an entire date by typing just numerals (01012008 to get January 1st, 2008). Not only the default Rails input a cumbersome interface, you gotta merge those fields together come controller time since Rails doesn’t handle this out of box (or does it and I just can’t find the right helpers?)
Back to Basics
start_date = Chronic.parse(data[:start_date], :guess => true, :context => :past) end_date = Chronic.parse(data[:end_date], :guess => true, :context => :past) raise "Invalid date format" if start_date.nil? || end_date.nil?
It would definitely be better to parse and validate through definition of a validation rule declaration. As I learn more Ruby, I will definitely revisit this with a cleaner approach, but at least I have a solid foundation for flexibly entering dates without slowing down the user or encumbering them with toys they won’t use.
Changing the Rails default format
Along the way to getting to know Rails, I wasn’t quite sure how to implement date formatting for all my date fields. The default rendering gave me “YYYY-MM-DD HH:MM,” which is a date plus a time! My first reaction was to shelter things behind a property (attribute getter/setter) with the following:
def formatted_start_date begin start_date.to_date.strftime('%m/%d/%Y') rescue '' end end
But I realized this was quickly going to get out of hand and started looking into how I could at least put the constant “%m/%d/%Y” into a globally accessible file. There were a lot of discussions about how setting the default formatting would mess up the database persistence mechanism, at least up to around Rails 1.2. Nowadays, the database libraries appear to use the “:db” format, thus leaving us free to set a :default we want for presenting to the users. I did this by creating a new file, “my_rails_project/config/initializers/format_strings.rb” and added the following:
# Sets default display of dates to mm/dd/yyyy format ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge! \ :default => "%m/%d/%Y"
This takes care of the Date class, and that’s all I really need, but you can also override the Time default by merging to ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS. Once doing this, my property getters are reduced essentially to guaranteeing I’m type coercing to a Date class (and not a Time class).
a.k.a. Code Connoisseur
- [email protected]
- ICQ ‐ 25239620
- AIM ‐ mwlang88
- Yahoo! ‐ mwlang88
- Google ‐ mwlang
- Twitter ‐ @mwlang88
EducationBachelor of Science
Information and Computer Science
- On Hiring Good People
- Week Five in the Gym
- The True Power of the Internet
- Rekindling a desire to workout consistently
- I'd Rather Eat my Britches than Do This
- Mold Killer Recipe
- Gonna be Starting Something New
- Pitch Camp, what is it good for?
- Less communication can be more
- Let the Musings Begin
- Working on a Referral Pre-Launch Site
- Making Commitments, Reaching Out
- Preparing for Countdown
- Ground Zero
- A Reflection of the Technologies Built Things With
- Dynamic Routing in Rails Revisited
- Creating Dynamic Routes at runtime in Rails 4
- Adding Google Analytics script to Sprockets
- Gems you should consider for every Rails projects
- Weak Password will get you Hacked!
- Status updating...