I am reading I18N for Active Record Models but I can't get an attribute on my model to display correctly.
I have a model like this:
class Tran < ActiveRecord::Base
validates :description, :presence => true,
:length => {:maximum => 100 }
end
and a en.yml file like this:
en:
activerecord:
attributes:
tran:
description: "Description"
errors:
models:
tran:
attributes:
description:
errors:
messages:
blank: "can't be blank!"
When I show the error message on the client, the "can't be blank!" successfully shows up, but the description does not change to "Description" like I would expect. What am I doing wrong?
Thanks.
This is pretty relevant to what you want :
How to change validation messages on forms
Related
I have the following validation:
validates :email, format: { with: Devise::email_regexp, message: I18n.t('activerecord.errors.models.client.attributes.email.invalid') }
The problem is, it is now showing the error message I specify there. It shows the default one.
What am I missing?
i18n should not be included in the model, but rather in a YAML file.
en:
activerecord:
errors:
models:
my_model:
attributes:
whatever:
inclusion: "Please select whatever." # see default key: "inclusion"
See more detailed answer about it here
At the moment, if I leave a field in the form blank, I get an error message that is written in the en.yml file, how can I overwrite this error message in the model?
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)#([\w\-]+\.)+([\w]{2,})\z/i
attribute :message, :validate => true
attribute :nickname, :captcha => true
This is what I've tried for the name attribute but I'm still getting the error message which is written in the en.yml file. I can't change the error message from en.yml as it is for another part of my application.
validates :name, presence: { message: "Can't be blank" }
Any ideas why this is not overwriting the message?
You can customize the error message using i18n localization in the same way that you would for a regular ActiveRecord model. The only difference is that you use the mail_form top-level scope instead of active_record.
# en.yml
mail_form:
errors:
models:
contact:
attributes:
name:
blank: "My custom message goes here"
sources:
MailForm i18n Documentation
Translations for ActiveRecord Models
You don't need to nest the message in an inner hash. The syntax for messages on validations is:
attribute :name, validate: true
Not very elegant, but you can set the error message manually:
contact = Contact.new
contact.valid? # => false
contact.errors[:name] = "Can't be blank" # => Will add "Can't be blank to the list of errors associated with name"
Or, if you want to replace the original error:
contact.errors.set(:name, "Can't be blank")
If the above answers are not working (try them out first, rails is good with error messages) you could use JQuery to validate the fields before submit, there is a plugin for that https://jqueryvalidation.org/
From there site
$("#myform").validate({
submitHandler: function(form) {
// some other code
// maybe disabling submit button
// then:
$(form).submit();
}
});
I'm using Stripe within a model class. I have a strike_token attribute that I am validating presences of and creating a custom message like so:
validates :stripe_token, presence: { message: 'Please enter valid card info' }
When the form is submitted and not valid, the error message is, "Stripe token Please enter valid card info"
I don't want Stripe token in the error message. I have other validations as well that would be suited for the default, so I don't want to change the default error behavior.
It would be better if I could alias the Stripe token attribute to something like :card_info without actually having that as an attribute on the model.
So the message could be made to read, "Card info cannot be blank"
You can do as given below:
Just remove the :message part from validation.
# config/locales/en.yml
en:
activerecord:
attributes:
model_name:
stripe_token: ""
errors:
models:
model_name:
attributes:
stripe_token:
blank: "Card info cannot be blank!"
OR
# config/locales/en.yml
en:
activerecord:
attributes:
model_name:
stripe_token: "Card info"
errors:
models:
model_name:
attributes:
stripe_token:
blank: " cannot be blank!"
Replace model_name with your model name in small letters. Please check Fully custom validation error message with Rails for more details.
Update
# config/locales/en.yml
en:
activerecord:
attributes:
model_name:
stripe_token: "Card info"
Then use,
validates :stripe_token, presence: { message: ' cannot be blank!' }
Hope it helps :)
I know you can do this:
# config/locales/en.yml
en:
activerecord:
attributes:
user:
email: "E-mail address"
errors:
models:
user:
attributes:
email:
blank: "is required"
via https://stackoverflow.com/a/2859275/718050
Question 1
Is it possible to specify the message for blank across an entire model, or even sitewide, instead of going into every single attribute?
Question 2
Also, it seems that blank comes from :presence in the model, e.g.
validates :email, :presence => true
So if a :presence => true error translates to blank:, where can I find a list of these translations? How am I supposed to know what :unique => true turns into inside en.yml?
This list is here and here
as you can see you can redefine blank error like this:
en:
errors:
messages:
blank: "can't be blank"
I am using Ruby on Rails 3.1 and I would like to know how to correctly handle internationalization related to "resources of resources". That is, ...
... in my config/routes.rb file I have:
resources :users do
resource :account
end
... in my app/models/users/account.rb file I have:
class Users::Account < ActiveRecord::Base
validates :firstname,
:presence => true
...
end
... in my config/locales/models/user/account/en.yml file I have:
en:
activerecord:
errors:
messages:
presence: "custom presence message - english"
... in my config/locales/models/user/account/it.yml file I have:
it:
activerecord:
errors:
messages:
presence: "custom presence message - italian"
The above code doesn't display in front end the "custom presence message" (it still displays the default RoR presence message: can not be blank). Furthermore if in my app/models/users/account.rb file I use:
class Users::Account < ActiveRecord::Base
validates :firstname,
:presence => { :message => t(:presence) } # Here I try to use the i18n helper method
...
end
I get the following error:
NoMethodError (undefined method `t' for #<Class:0x000001075bbc80>)
Why I get the NoMethodError?
Is the problem related to how I am organizing in directories my locale files? At this time (as stated in the official RoR guide) my file system is:
config/locales/defaults/en.yml
config/locales/defaults/it.yml
config/locales/models/user/en.yml
config/locales/models/user/it.yml
config/locales/models/user/account/en.yml
config/locales/models/user/account/it.yml
In few words, I would like to display my "custom presence message" only on validating "resources of resources" kind of Users::Account. How can I do that?
I also tried to state the following code in the config/locales/models/user/account/en.yml file
en:
activerecord:
errors:
models:
user:
account:
attributes:
firstname:
blank: "custom presence message - english"
but it doesn't work. Anyway the following works but I need different translations for different attributes (as I tried to state in the previous code example):
en:
activerecord:
errors:
messages:
blank: "custom presence message - english"
ok, due to this: https://github.com/rails/rails/issues/1402 last comment nested model look-up is removed
so try maybe something like
activerecord:
errors:
models:
users:
account:
attributes:
first_name:
blank: "You should fill up first name field to complete that"
and change inside :message hash to
I18n.t(:"activerecord.errors.models.users.account.attributes.first_name.blank")
and try avoid nested models ;-)
update:
after some debuging this will work:
activerecord:
errors:
models:
"users/account":
attributes:
first_name:
blank: "You should fill up first name field to complete that"