Abhijith
Abhijith

Reputation: 2632

Non active record model validation i18n

I am using a model that is not a child of ActiveRecord. I would like to internationalize the validation error messages.

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

If i just use :message => I18n.t(:'errors.models.user.attributes.phone.invalid') it always returns the string from en.yml irrespective of the locale of the user.

I would like to avoid using ActiveModel::EachValidator since it doesn't work with client side validations.

Upvotes: 1

Views: 571

Answers (1)

Abhijith
Abhijith

Reputation: 2632

I found that using the following works as expected.

en.yml

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

user.rb

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

Upvotes: 2

Related Questions