I'm trying to do something very simple. I have a validates presence of in rails 3:
validates_presence_of [:first_nm]
When this fires, it gives the following crappy message:
"first nm cannot be blank"
I want to override the error message to give a friendly field name for "first nm"
"Please fill out your First Name"
I have seen all kinds of plugins, localization, humanized attributes tutorials, but these don't seem to work or are outdated. Is there no simple way to do this in Rails 3?
For localization, i've tried this:
# Sample localization file for English. Add more files in this directory for other locales.
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:
activerecord:
errors:
messages:
taken: "has already been taken"
record_invalid: "Validation failed: %{errors}"
models:
customer:
blank: "This is a custom blank message for %{model}: %{attribute}"
attributes:
first_nm:
blank: "This is a custom blank message for first name"
Alas, no luck. My error message did not change.
On thing that might be related. I'm not inheriting from ActiveRecord, because this object is getting saved via soap, not database. Instead, I have the following:
class Customer
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
end
Have you seen this page on ActiveRecord localization? It seems to be for Rails 3. I can't test it right now, but by the document it seems you can do:
models.user.attributes.first_nm.blank = "Please fill out your First Name"
Related
How do you overwrite a validation message in Rails?
Even if I pass in a proc like so..
validates :my_item, uniqueness: {
scope: [:name, :type],
message: -> (object, data) do
"This item is already taken"
end,
}
It still appends the [object] to the front of the message. So that is reads: "[name of the object] This item is already taken"
Edit:
Also have tried adding a ^ to the beginning of message. Still does not work.
"^This item is already taken"
Instead of trying to override it at the model level, just leverage the fact that you're essentially trying to tweak the en locale.
In config/locales/en.yml you should be able to do something like this:
en:
activerecord:
errors:
messages:
taken: "This item is already taken."
models:
my_item:
taken: "This item is already taken."
I haven't tested this config directly, but the messages key should change the message for all models. Otherwise you can specify the message on a per-model basis. I don't believe that this will include the object name. You can find a list of the field names in the Rails Internationalization Guide.
I'm trying customize validation error messages using i18n.
Let's say I have an Address model that validates the presence of a zip_code.
My config/locales/activerecord.fr.yml looks like this
fr:
activerecord:
errors:
models:
address:
attributes:
civility:
blank: "Some message"
The issue if I fail the validation, the error message I'l have in #address.errors.full_messages will be:
"Zip code Some message"
Why does Zip code gets added to my error message ? And how can I avoid this behaviour ?
You can just add:
errors:
format: "%{message}"
This way you will just show error message with out attribute name.
Edit: this should be added to config/locales/fr.yml and not config/locales/activerecord.fr.yml (which also has errors:)
Found a gem which will solve your purpose
With the help of this gem, You just need to start the locale message with a caret and it shouldn't display the attribute name in the message.
A model defined as:
class Item < ApplicationRecord
validates :name, presence: true
end
with the following en.yml:
en:
activerecord:
errors:
models:
item:
attributes:
name:
blank: "^You can't create an item without a name."
item.errors.full_messages will display:
#You can't create an item without a name
instead of the usual
#Name You can't create an item without a name
You could simply use:
#address.errors.messages[:zip_code] # or #address.errors[:zip_code]
#=> ['Some message']
I've three models called Account,User and AccountPermission.
I'm creating the Account via AccountPermission while creating user.
However, If a problem occurs related with Account :name, the system throws something like that below.
Account permissions account name has already been taken
So, I just need to fix this error message.
I've tried to add a message attribute to my validation. It is just appending to actual message.
I've also tried locale thing. Still just appending
en:
activerecord:
errors:
models:
account:
attributes:
name:
taken: 'bla bla'
As far as I see in ActiveModel. This message's structure comes from below
locale/en.yml in ActiveModel
en:
errors:
format: "%{attribute} %{message}"
So, Is there any way edit this message painless ? If I even delete the model name, It's enough.
Error message is concatenated from the error itself and the attribute name, which is account_permissions/account.name in the provided error.
You can add locale for your attribute names like so:
en:
activerecord:
attributes:
account: # this is model name
name: "Name"
or
en:
activerecord:
attributes:
account_permissions/account:
name: "Account name"
I have a Post.body field that is required in the model but in the UI I label this field as Details in the form.
The error rails generated is Body can't be blank.
But I really want to display that error as Please enter some Details.
How can I customize model errors in Rails 4?
The solution for a mountable engine turned out to be hidden under a giant rock...
# /config/locales/en.yml
en:
activerecord:
attributes:
'my_engine/my_model':
my_field: "Details"
errors:
models:
'my_engine/my_model':
attributes:
my_field:
blank: "can't be blank"
This should be in the documentation for mountable engines.
From the Active Record Validations guide:
[...] the :message option lets you specify the message that will be added to the errors collection when validation fails. When this option is not used, Active Record will use the respective default error message for each validation helper.
So you should be able to say:
validates :body, presence: true, message: 'Please enter some Details'
in your model.
This has been bugging me for a while. This problem occurs with all of my models, but i'll use one of them, Quiz, as an example.
Quiz has the following validations:
validates_presence_of :size, :style
I'm using I18n, and i have the following set in my translations file: (these are just the standard error messages, but i've included them in my en.yml so that it's easy to see the structure, if i want to override them for any particular model)
activerecord:
errors:
messages:
inclusion: "{{attribute}} is not included in the list"
invalid: "{{attribute}} is invalid"
empty: "{{attribute}} can't be empty"
blank: "{{attribute}} can't be blank"
record_invalid: "Validation failed: {{errors}}"
The problem is this: if i make a new quiz, which will fail validation, then look at quiz.errors.full_messages, each error message has the attribute then the full message:
>> quiz = Quiz.create
=> <unsaved quiz object>
>> quiz.errors.full_messages
=> ["Size Size can't be blank", "Style Style can't be blank"]
I don't understand why the message is, for example, "Size Size can't be blank" and not "Size can't be blank"
Any ideas anyone?
There should be also:
en:
errors:
# The default format to use in full error messages.
format: "%{attribute} %{message}"
And your other translations shouldn't include %{attribute} anymore.
To make sure you get all correctly use en.yml from your Rails version,
it is located at: lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/locale/en.yml
I just figured this out and thought i'd answer it myself in case anyone else had this problem: i had to amend the activerecord part of my translations file thus:
activerecord:
errors:
full_messages:
format: "{{message}}"
#define standard error messages, which we can overide on per model/per attribute basis further down
messages:
inclusion: "{{attribute}} is not included in the list"
exclusion: "{{attribute}} is reserved"
The problem was that the activerecord.errors.full_messages.format key was set (in vendor/rails/activerecord/lib/active_record/locale/en.yml) to "{{attribute}} {{message}}", and the messages in turn were set to "{{attribute}} can't be blank" for example. So the full_message was coming out as "{{attribute}} {{attribute}} can't be blank". Changing it to be just "{{message}}" fixed this.