details hash sets :error to the message instead of the validator type
nrser opened this issue · comments
Neil Souza commented
When i do
validates :post_code, zipcode: true
the record.errors[:post_code]
object looks like
#<ActiveModel::Errors:0x000000057dec18 @base=#< ... post_code: "bad!", ...>, @messages={:post_code=>["Invalid ZIP / postal code"]}, @details={:post_code=>[{:error=>"Invalid ZIP / postal code"}]}>
where record.errors[:post_code].details[0]
is {:error=>"Invalid ZIP / postal code"}
instead of the Rails standard behvior of the "validator type" symbol (which would be :invalid_zipcode
in your case) (example).
This prevents translating the error into anything other than the default locale:
# config/locales/zh-CN.yml
zh-CN:
errors:
messages:
invalid_zipcode: 无效邮编号码
I18n.t "errors.messages.#{ record.errors.details[:post_code].first[:error] }", locale: 'zh-CN'
=> "translation missing: zh-CN.errors.messages.Invalid ZIP / postal code"
It's expected to produce a translation as it does with built-in validations:
I18n.t "errors.messages.#{ record.errors.details[:name].first[:error] }", locale: 'zh-CN'
=> "不能为空字符"
Is there any reason the library is using the string message instead of the "validator type" symbol?