0

The validates_timeliness gem says it supports I18n for its error messages. However, I can't find any configuration (or documentation) to show me how to modify the date format on error messages it generates.

There is a DEFAULT_ERROR_VALUE_FORMATS in the gem's validator.rb file. However, I don't want a global change -- I need to format error messages per locale.

I've tried adding the relevant I18n date format block to my application's config/locales language files, like so:

en:
  date:
    formats:
      default: "%d/%m/%Y"

However, that has no effect after server restart.

So my question: how can I apply a locale-specific date to the error messages that the validates_timeliness gem generates?

Ian M
  • 91
  • 1
  • 5

1 Answers1

1

I discovered that validates_timeliness has its own locale block which it defines in lib/generators/validates_timeliness/templates/en.yml, and to change format you need to override that block.

Add the following block to the locale file(s) in your application's config/locales directory:

en:
  validates_timeliness:
    error_value_formats:
      date: '%m/%d/%Y'
      time: '%H:%M:%S'
      datetime: '%m/%d/%Y %H:%M:%S'

The formatted sections can be any valid strftime-formatted string.

This answer specifically applies to a Rails 3.2 and validates_timeliness 3.0.14 environment, but likely applies generally.

Ian M
  • 91
  • 1
  • 5