2014-04-18 116 views
0

我使用的模型不是ActiveRecord的子模型。我想將驗證錯誤消息國際化。非活動記錄模型驗證i18n

class User 
    validates_format_of :phone, :with => /someregex/ , message => :'Incorrect phone number' 
end 

如果我只是用:message => I18n.t(:'errors.models.user.attributes.phone.invalid')它總是返回從en.yml不論用戶的語言環境的字符串。

我想避免使用ActiveModel :: EachValidator,因爲它不適用於客戶端驗證。

回答

0

我發現按照預期使用以下工作。

en.yml

activemodel: 
    errors: 
     models: 
     user: 
      attributes: 
      phone: 
       invalid: "PhoneNumber is invalid" 

user.rb

validates :phonenumber, :format => { with: /someregex/ } 
+0

但是,這將返回消息 '電話******中國無效' 的權利?這是你所期望的嗎? –

+0

何時:手機與正則表達式不匹配,錯誤消息只是說「PhoneNumber無效」。 – Abhijith

相關問題