简体   繁体   中英

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 USA 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.

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

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

you could use

ActiveSupport::TimeZone.us_zones.map(&:name) or ActiveSupport::TimeZone.us_zones.map{ |tz| tz.tzinfo.name } 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM