Formatting model.errors.full_messages [duplicate] - ruby-on-rails

This question already has answers here:
Fully custom validation error message with Rails
(18 answers)
Closed 5 years ago.
How do you format columns names in error messages?
class Person
validates_presence_of :name, :address, :email
validates_length_of :name, in: 5..30
end
person = Person.create(address: '123 First St.')
person.errors.full_messages
# => ["Name is too short (minimum is 5 characters)",
"Name can't be blank", "Email can't be blank"]
For example, when there is an error instead I want it to print
Full name can't be blank. (instead of 'Name')
How do I do this since in the model/database its stored as :name.
Is there someway I can link a string to :name?

You can specify a custom message.
validates_length_of :name, in: 5..30, message: 'Full name must be between 5 and 30 characters'
http://guides.rubyonrails.org/active_record_validations.html#message
You can also just translate the attribute
en:
activerecord:
attributes:
person:
name: "Full name"
By adding that to the config/locales/en.yml file
http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

Related

How to rename table in validates

I'm doing validations like this: validates :name_user, presence: true
But I would like to change the name of the field "name_user" in the message: "column name_user can not be blank" appears "column first name can not be blank"
set up a translation for "name_user" in your locales. You can change both the name of the field and what the exact wording of the error message should be.
# config/locales/en.yml
en:
activerecord:
attributes:
user:
name_user: "First name"
errors:
models:
user:
attributes:
name_user:
blank: "is required"
You can extend it for additional fields and even other models...
# config/locales/en.yml
en:
activerecord:
attributes:
user:
name_user: "First name"
surname_user: "Last name"
department:
created_year: "Year established"
errors:
models:
user:
attributes:
name_user:
blank: "is required"
You can use the message option for this, as described here in the Rails guide.
validates :name_user, presence: { message: "First name cannot be blank" }
Now all your errors will use that strange. You can even make reference to the value that was passed in (as described in the Rails guide).

Conditional validation if another validation is valid [duplicate]

This question already has answers here:
Rails validate uniqueness only if conditional
(3 answers)
Closed 6 years ago.
I have the following three validations in a model in my rails app:
validates_presence_of :reset_code, message: 'Please enter your reset code'
validates_length_of :reset_code, is: 4, message: 'Your reset code should be 4 digits'
validates_format_of :reset_code, with: /\A[0-9]{4}\z/, message: 'Please enter a valid reset code'
I only want to fire the second and third validations if the first is valid (as there is no point in telling the user the reset code isn't the correct length or format if they haven't entered one at all).
So something along the lines of:
validates_length_of :reset_code, is: 4, message: 'Your reset code should be 4 digits', :if => :reset_code.present?
You should use new the new validation syntax, and provide a simple if: condition:
validates :reset_code, length: { is: 4 },
message: 'Your reset code should be 4 digits',
if: -> { reset_code.present? }
validates :reset_code, format: { with: /\A[0-9]{4}\z/ },
message: 'Please enter a valid reset code',
if: -> { reset_code.present? }

Multilingual support for custom validation messages in Rails

Hi let's say I have the following in my accounts model:
validates :name, length: {in: 1..70, message:%Q|Please enter a decent name Sr.|}
How can I add multilingual support to those custom validation messages? I checked this tutorial
But I could not find out how to translate custom validation messages in the model.
I needed once to use translations in model so I went this way:
TITLE = { 0 => :"employee.title.mrs",
1 => :"employee.title.mr",
2 => :"employee.title.miss" }
these are options for select, and in select I used t(value_of_key_here), value was a string that was seen as a key to locale.
So in your case this might work (not really sure):
validates :name, length: {in: 1..70, message: :"enter_decent_name"}
that would return a key in your validation messages and rails will just complain about missing key in translations that you have to add into your yml file:
enter_decent_name: 'Please enter a decent name Sr.'

Attributes in locale file not registering in Rails 4

I am making a Rails app to replicate my currently-online arcade website that I wrote in PHP. The only controller I have right now is 'games' and I have some fields such as title and description. When I submit the form and I don't meet the requirements of the validators I have set in my models/game.rb, it will show a red bar underneath containing the error, such as " is too short (minimum is 20 characters)." For some reason, it doesn't put the field name in front of the word 'is'.
Yes, it is obvious when you look at it that it is referring to the field above, but there is definitely something wrong here. I have created other Rails apps in the past with the same issue and there actually hasn't been a time in the past few months that I've been playing with Rails that I've seen this working right.
Screenshot here: http://postimg.org/image/v3kt6xia1/
Below is my code.
models/game.rb
class Game < ActiveRecord::Base
$msg_success_icon = "fa fa-check fa-lg"
$game_categories = ["Adventure", "Arcade", "Action", "Racing", "Puzzle", "Strategy", "Sports", "Shooting", "Misc", "Movies"]
validates :title, {
:presence => true,
:length => {
:minimum => 2
}
}
validates :description, {
:presence => true,
:length => {
:minimum => 20
}
}
end
config/locales/en.yml
en:
activerecord:
models:
game:
one: Game
other: Games
attributes:
game:
title: 'Title'
description: 'Description'
game_width: 'Width'
game_height: 'Height'
category: ''
errors:
models:
game:
attributes:
title:
blank: 'okay fill that title field in!'
embed_url:
blank: 'must link to the game file (.swf & .dcr extensions allowed)'
thumb_url:
blank: 'must link to a image of the game (.jpg, .gif, & .png allowed)'
category:
inclusion: ''
messages:
too_short:
one: must be at least 1 character long.
other: must be at least %{count} characters long.
not_a_number: must be a number.
I compared this code to several examples that I found online and I can't figure out what I'm doing wrong. I feel like the problem lies elsewhere but I don't know where. Any help would be much appreciated.

How can I translate the ActiveRecord attribute name in Rails 3.2?

I have the following validation in my model ( User ):
validates :first_name,:length => {:minimum => 2,:maximum => 50},:format => { :with => /[a-zA-Z]+/ }
I have the following in my yml locales file:
attributes:
first_name:
too_short: "First name is too short"
too_long: "First name is too long"
invalid: "First name is not valid"
Now, if I start a rails console, and write the following:
a = User.new
a.valid?
a.errors.full_messages
I will see the following errors:
["First name First name is too short", "First name First name is not valid"]
As you can see, the attribute name is also prepended to the field error. So far, everywhere in my code, I have used model.errors[:field], and this will always show me the string I have in the yml file, but I'd like to change the strings to:
attributes:
first_name:
too_short: " is too short"
too_long: " is too long"
invalid: " is not valid"
And use the full_messages version. The problem is, I don't know how to translate the attribute name. Let's say for example, that I'd like instead of First name, to have Name first. How would I do it?
You can find the answer here http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
in your config/locale/(your_lang).yml
en:
activerecord:
models:
user: Dude
attributes:
user:
first_name: "Name first"
change "en:" with the language symbol that you need to use
cheers
In Rails 5, after attributes, I had to namespace with my model's name in underscore. Like this:
pt-BR:
activerecord:
attributes:
my_model_name_in_underscore:
attribute_name: 'String'
Source: https://stackoverflow.com/a/5526812/1290457

Resources