Modify error messages for validation with an option - ruby-on-rails

I have already modified quite a few option error messages as shown below
en:
activerecord:
attributes:
user:
name: "Name"
email: "E-mail address"
password: "Password"
password_confirmation: "Password confirmation"
errors:
messages:
blank: "%{attribute} is required"
taken: "%{attribute} is already taken"
invalid: "%{attribute} is not valid"
models:
user:
attributes:
password:
confirmation: "%{attribute} doesn't match confirmation"
Yet, I don't know how I can modify the error message for the length when it's not within a specified range. I know it's possible to do so (http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models) but I don't know how?
Thanks,

I found these tags in my app. One of those should work for you!
equal_to: must be equal to %{count}
greater_than: must be greater than %{count}
greater_than_or_equal_to: must be greater or equal to %{count}
less_than: must be less than %{count}
less_than_or_equal_to: must be less or equal than %{count}
too_long:
one: 'is to short (max than: 1 character)'
other: 'is to long (max than: %{count} characteres)'
too_short:
one: 'is to short (min: 1 character)'
other: 'is to short (min: %{count} characteres)'
wrong_length:
one: doesn't have the right length (1 character)
other: doesn't have the right length (%{count} characteres)
other_than: must be different than %{count}

Related

Error translation Rails

There is a piece of some translation file:
errors:
format: "%{attribute} %{message}"
messages:
too_short: "must be greater then ..."
I want that my 'too_short' message must show message with length value from Active Record validation rule. Is it possible? Thanks.
Try this.
errors:
format: "%{attribute} %{message}"
messages:
too_short: "must be greater than %{count}"

Modify Devise reset password error text

Using the Devise GEM, when a user password is reset they are allowed to set a new password. If the entered passwords do not match or if the password is too short, you get default messages:
•Password doesn't match confirmation
•Password is too short (minimum is 8 characters)
How/where can I change the text of these error messages?
Add this to your config/locals/en.yml and change it to what you want
en:
activerecord:
errors:
models:
user:
attributes:
password:
confirmation: "Password does not match"
too_short: "is too short (minimum is %{count} characters)"
attributes:
user:
password: "Password"

All possible model validation errors

I have got a form with a bunch of fields and model validations.
How can I return all possible validation errors that can be raised?
I need it to write locales for all of them.
I want to get a list like this:
password blank
password too_short
password confirmation
login blank
login invalid
email blank
email too_short
email invalid
etc
Basically what Pablo says, except that the page on the rails docs doesn't show how to override the messages for a particular model and field. here's an example from one of my apps:
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"
invalid: "{{attribute}} is invalid"
confirmation: "{{attribute}} doesn't match confirmation"
accepted: "{{attribute}} must be accepted"
empty: "{{attribute}} can't be empty"
blank: "{{attribute}} can't be blank"
too_long: "{{attribute}} is too long (maximum is {{count}} characters)"
too_short: "{{attribute}} is too short (minimum is {{count}} characters)"
wrong_length: "{{attribute}} is the wrong length (should be {{count}} characters)"
taken: "{{attribute}} has already been taken"
not_a_number: "{{attribute}} is not a number"
greater_than: "{{attribute}} must be greater than {{count}}"
greater_than_or_equal_to: "{{attribute}} must be greater than or equal to {{count}}"
equal_to: "{{attribute}} must be equal to {{count}}"
less_than: "{{attribute}} must be less than {{count}}"
less_than_or_equal_to: "{{attribute}} must be less than or equal to {{count}}"
odd: "{{attribute}} must be odd"
even: "{{attribute}} must be even"
record_invalid: "Validation failed: {{errors}}"
models:
quiz:
blank: "{{attribute}} can not be blank"
user_session:
blank: "{{attribute}} can not be blank"
attributes:
login:
invalid: "Please enter your user name"
password:
invalid: "Please note that passwords are case sensitive"
I've also changed the basic format for error messages, as sometimes i didn't want the field name shoved at the start of the message. So, i changed
errors:
format: "{{attribute}} {{message}}"
to
errors:
format: "{{message}}"
Which is why i then specify {{attribute}} in my subsequent errors, to put it back in in most but not all cases.
Note also that i'm using the old syntax of {{var}} rather than %{var}. The same principles apply though.
The latest rails translations are at: rails-i18n.
ActiveRecord errors are under lang:errors and lang:active_record in each .yaml.
Also in your application under config/locales/en.yml is the default.

RoR=> Strange validate message

I'm a newbie in rails, today I made my first web application using the validate, I just put this lines into the model:
class ClientWorkout < ActiveRecord::Base
validates_numericality_of :paid_amount
validates_presence_of :client_name
end
This is the view part:
<% form_for(#client_workout) do |f| %>
<%= f.error_messages %>
etc etc
Everything works fine, and the value are stored in the db, in succesful case< if an error occour, instead, this error displays on the view in this strange following manner:
{{count}} errors prohibited this {{model}} from being saved
There were problems with the following fields:
{{attribute}} {{message}}
{{attribute}} {{message}}
(The example show what's happening when 2 parameters of the form are wrong, but this happen in every case)
It doesen't manage to replace "count, model, attribute and message" with the real value.
Anyone can figure out about what's happenned ?
I use Ror 2.3.8 and rails 1.8.7
Rails introduced built-in internationalization back in 2.3. Your issue is a known bug with some combinations of rails and the i18n gem. If you have i18n gem version 0.5.0, try downgrading to 0.4.2. If you're using system gems:
sudo gem uninstall i18n
sudo gem install i18n -v 0.4.2
If you're using RVM to manage your gems, you don't need the sudo command.
If you are not interested in changing the i18n version you can do the following
Add the below code in config/locales/en.ym
If en: is already available copy and paste from ActiveRecord
After that stop the server and start again that should display the error messages properly...
en:
activerecord:
errors:
full_messages:
format: "%{attribute} %{message}"
messages:
inclusion: "is not included in the list"
exclusion: "is reserved"
invalid: "is invalid"
confirmation: "doesn't match %{attribute}"
accepted: "must be accepted"
empty: "can't be empty"
blank: "can't be blank"
too_long: "is too long (maximum is %{count} characters)"
too_short: "is too short (minimum is %{count} characters)"
wrong_length: "is the wrong length (should be %{count} characters)"
not_a_number: "is not a number"
not_an_integer: "must be an integer"
greater_than: "must be greater than %{count}"
greater_than_or_equal_to: "must be greater than or equal to %{count}"
equal_to: "must be equal to %{count}"
less_than: "must be less than %{count}"
less_than_or_equal_to: "must be less than or equal to %{count}"
other_than: "must be other than %{count}"
odd: "must be odd"
even: "must be even"
template:
header:
one: "1 error prohibited this %{model} from being saved"
other: "%{count} errors prohibited this %{model} from being saved

Is there a default english translation file for Active Record?

I am upgrading an application a rails application to 2.3.2 and I am finding that I can't display the default validation error messages for ActiveRecord because I don't have a translation file for it.
This is the error that is reported:
translation missing: en-US, activerecord, errors, template, header
translation missing: en-US, activerecord, errors, template, body
Email translation missing: en-US, activerecord, errors, models, user, attributes, email, taken
Does anyone know where I can find a default English translation file that would include all the strings that the validations might use?
This happened because my language setting was 'en-US' and not 'en'. There are translation files under activerecord/lib/locale. I copied these translations into a new file en_US.yml.
"en-US":
activerecord:
errors:
template:
body: There were problems with the following fields
header:
one: 1 error prohibited this {{model}} from being saved
other: "{{count}} errors prohibited this {{model}} from being saved"
messages:
inclusion: "is not included in the list"
exclusion: "is reserved"
invalid: "is invalid"
confirmation: "doesn't match confirmation"
accepted: "must be accepted"
empty: "can't be empty"
blank: "can't be blank"
too_long: "is too long (maximum is {{count}} characters)"
too_short: "is too short (minimum is {{count}} characters)"
wrong_length: "is the wrong length (should be {{count}} characters)"
taken: "has already been taken"
not_a_number: "is not a number"
greater_than: "must be greater than {{count}}"
greater_than_or_equal_to: "must be greater than or equal to {{count}}"
equal_to: "must be equal to {{count}}"
less_than: "must be less than {{count}}"
less_than_or_equal_to: "must be less than or equal to {{count}}"
odd: "must be odd"
even: "must be even"
Then I just added my custom strings after these.
You can avoid copying and pasting the standard translations by telling I18n to fallback to :en when it can't find a translation in :en_US. The example initializer below shows how we did it for falling back from 'en-GB' and 'it-IT' to the standrd 'en', throwing in pluralizations for good measure
# config/initializer/i18n.rb
I18n.backend = I18n::Backend::Chain.new(I18n.backend)
I18n::Backend::Chain.send(:include, I18n::Backend::Fallbacks)
I18n.fallbacks[:'en-GB'] << :en
I18n.fallbacks[:'it-IT'] << :en
require "i18n/backend/pluralization"
I18n::Backend::Chain.send(:include, I18n::Backend::Pluralization)
FYI, if you are including these in the old style hash format, use this;
:activerecord => {
:errors => {
:template => {
:body => 'There were problems with the following fields',
:header => {
:one => '1 error prohibited this {{model}} from being saved',
:other => "{{count}} errors prohibited this {{model}} from being saved"
}
},
:messages => {
:inclusion => "is not included in the list",
:exclusion => "is reserved",
:invalid => "is invalid",
:confirmation => "doesn't match confirmation",
:accepted => "must be accepted",
:empty => "can't be empty",
:blank => "can't be blank",
:too_long => "is too long (maximum is {{count}} characters)",
:too_short => "is too short (minimum is {{count}} characters)",
:wrong_length => "is the wrong length (should be {{count}} characters)",
:taken => "has already been taken",
:not_a_number => "is not a number",
:greater_than => "must be greater than {{count}}",
:greater_than_or_equal_to => "must be greater than or equal to {{count}}",
:equal_to => "must be equal to {{count}}",
:less_than => "must be less than {{count}}",
:less_than_or_equal_to => "must be less than or equal to {{count}}",
:odd => "must be odd",
:even => "must be even"
}
}
}

Resources