LearningRoR
LearningRoR

Reputation: 27212

How to validate inclusion of time zone?

When I try:

validates_inclusion_of :time_zone, :in => TimeZone
validates_inclusion_of :time_zone, :in => Time.zone

This error appears:

"<class:User>": uninitialized constant User::TimeZone (NameError)

I'm trying to let users select any time zone of the world but since I'm based in the U.S.A. my select menu is this:

<%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones, {:prompt => "Select Your Time Zone *"}, {:id => "timezone"} %>

What's the correct way to do this?

Thank you.

Upvotes: 2

Views: 2377

Answers (3)

Sakthivel M
Sakthivel M

Reputation: 39

you could use

ActiveSupport::TimeZone.us_zones.map(&:name) or ActiveSupport::TimeZone.us_zones.map{ |tz| tz.tzinfo.name }

to list the time zone in the select menu and you could add a similar custom validation mentioned above. Like,

validate :check_timezone

def is_proper_timezone
  errors.add(:time_zone, 'invalid') unless ActiveSupport::TimeZone[time_zone]  
end

Upvotes: 1

lulalala
lulalala

Reputation: 17981

I think here is a better solution:

validate :time_zone_check
def time_zone_check
  # I allow nil to be valid, but you can change to your likings.
  if time_zone && ActiveSupport::TimeZone.new(time_zone).nil?
    errors.add(:time_zone)
  end
end

The reasons are:

  1. Rails caches existing TimeZone objects, which new uses. So using it to lookup objects will not create extra objects. (ActiveSupport::TimeZone.us_zones.map(&:to_s) definitely will)

  2. If you happen to import time_zone from else where (such as browser user-agents), you will get TZInfo identifiers, which may not be in ActiveSupport::TimeZone.all. The related issue is here: https://github.com/rails/rails/issues/7245

Upvotes: 5

deefour
deefour

Reputation: 35360

If you select the option with value "(GMT-05:00) Eastern Time (US & Canada)" this string is going to be passed to the model for validation. Your validates_inclusion_of is going to run Enum's .include? method on the collection you pass with :in.

Neither Timezone and Time.zone extend Enum to my knowledge, so they will not return an Enum instance that .include? will return true/false for.

If your select consists of ActiveSupport::TimeZone.us_zones, this is what you should be checking the inclusion validator against

validates_inclusion_of :time_zone, :in => ActiveSupport::TimeZone.us_zones

But as ActiveSupport::TimeZone.us_zones doesn't return strings, one way you could get a common type for comparison is casting the above Enum's contents to strings.

validates_inclusion_of :time_zone, :in => ActiveSupport::TimeZone.us_zones.map(&:to_s)

With this, a selected value like "(GMT-05:00) Eastern Time (US & Canada)" should evaluate true, as the following does in console without trouble.

> ActiveSupport::TimeZone.us_zones.map(&:to_s).include?("(GMT-05:00) Eastern Time (US & Canada)")
=> true

Upvotes: 4

Related Questions