← Back to Upcase

Adjusting raw date entry for saving to model


(Jon Seidel) #1

What’s a good way to handle date entry into a form and make sure that the year is specified/interpreted correctly? Currently, I have a hook in my model which examines date fields and handles them with this code:

self.start_date = Date.new(start_date.year + 2000, start_date.month, start_date.day) if start_date.year < 2000

It works, but it seems pretty hackish


(Ben Orenstein) #2

I’d probably handle this by using a datepicker. Makes it less likely that users will enter bad data.

If you want to keep it free text, I’d at least move this into a validation. Show the user an error if the year isn’t 4 digits.

Your current solution looks like it could lead to surprising results If someone enters “88”, do you think they mean 2088 and not 1988? Hard to say.

I’d complain to the user and ask them to fix it.


(Jon Seidel) #3

Thanks; I do have a validation for a valid date with a custom message. I’ve suggested the datepicker. I could also allow a ‘sliding range’ of date values, say 1970 - 2069 without much problem. But simple text entry is more efficient as the user doesn’t have to take their hands off the keyboard.