views:

238

answers:

3

Hi, i have a form where my users can register to my site. They fill in theirs birthdate in the form: birthyear, birthmonth and birthday. So i am using Range to create the select in the form like this:

= f.select(:birthmonth, options_for_select((1..12)))

But that doesnt start the single digit numbers with a zero like i want: 01, 02, 03, 04, 05, 06, 07, 08, 09, 10 and so on..

I have even tried this but it didnt work:

= f.select(:birthmonth, options_for_select((01..12)))

Anybody that know how to get Range to start with leading zeros? or any other way to do whis so i can use it in the validation?:

validates_inclusion_of :birthmonth, :in => 1..12

Thanks, Micke

A: 

Try padding numbers that are less than 10, like this:

= f.select(:birthmonth, options_for_select((1..12).collect{ |i| i < 10 ? "0#{i}" : i }))
Jimmy Baker
Thanks! can i use that in the validation to?
Micke
As a result of this hack, you'll have mixed types in the resulting array, some of the elements being converted to strings, others left as integers. Although it probably wouldn't cause any problem in this particular case, this kind of hacking shouldn't ever be considered as an option, at least not in Ruby (this looks like PHP habit to me).
Mladen Jablanović
so how should i do it?
Micke
Well, doesn't Chuck's solution work? I might add one more.
Mladen Jablanović
+8  A: 

The trouble is that numbers themselves don't have leading zeroes (or, alternately, have an infinite number of leading zeroes that are never printed). To get leading zeroes, you need strings. Fortunately, Ruby comes with this sort of string formatting ability built in.

= f.select(:birthmonth, options_for_select((1..12).map {|n| "%02d" % n}))

The %02d is a formatting specifier that means "pad it with leading zeroes if it's less than two digits."

For validation, you'll need to do the same conversion:

validates_inclusion_of :birthmonth, :in => (1..12).map {|n| "%02d" % n}

(Thanks to @r-dub for pointing out that bit.)

Chuck
You should probably add the correct validates statement to your answer for completeness. validates_inclusion_of :birthmonth, :in => (1..12).map {|n| "%02d" % n}
r-dub
It wont validate when i pick a number that starts with a zero. Numbers that starts with one validate though
Micke
Add the validation line I put in my last comment and it should work fine. Your current validation is not the same values as what Chuck put in his answer for the select box.
r-dub
@r-dub i tried yours but it wont work
Micke
This seems to work on the validation part. (1..12).map {|n| ("%02d" % n).to_s}
r-dub
Okay, will try that
Micke
It looks like it is because all months will be converted to strings. And it only validates integers
Micke
I edited my last comment with the correct expression that converts them to strings.
r-dub
I managed to get it working with Jimmy Bakers solution by changing it to this: (1..12).collect{ |i| i < 10 ? 0+i : i }
Micke
You were quicker than me! Thanks!
Micke
+1 for using the `sprintf` shortcut.
macek
+1  A: 

If it is just a presentational issue, you can construct [text, value] pairs and pass them to options_for_select:

Given a container where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and the "firsts" as option text.

So, something like:

options_for_select((1..12).map {|n| ["%02d" % n, n]})

That way you should be able to keep your validation logic as is.

Mladen Jablanović
+1 Probably the best answer, though a little late to the party.
r-dub