Reputation: 14701
Is there a way to customize message in Rails for confirmation fields? For example in devise I have to enter password and password_confirmation and error message is:
Password confirmation doesn't match Password
I could change active record locale message ("doesn't match") but it outputs Password confirmation and Password at the begin and the end of that locale message so I get something like this:
"Password confirmation must match Password"
Is there a way to change it to different string?
Password confirmation and Password must match.
EDIT
Another thing is to have completely custom message, for example:
'Set password' and 'Confirm password' must match.
Upvotes: 8
Views: 24843
Reputation: 4065
ActiveRecord en.yml is the answer I would suggest I you want to Change the Validation Message for Devise
Here How the en.yml should look like
en:
activerecord:
errors:
models:
user:
attributes:
email:
blank: "Please Specify an Email id"
taken: "Please use a different Email id"
invalid: "Please Specify a valid Email id"
password:
blank: "Please Specify a Password"
confirmation: "Password does not match"
password_confirmation:
blank: "Please Specify a Password Confirmation"
first_name:
blank: "Please Specify First Name"
last_name:
blank: "Please Specify Last Name"
pdf:
attributes:
name:
blank: "Please Specify name to PDF"
taken: "Please use different name for PDF"
attachment:
blank: "Please Upload a PDF Attachment"
data_element:
attributes:
name:
blank: "Please give Element a desired name"
taken: "Already Created Element with given name"
color:
blank: "Please assign a color to Element"
template:
attributes:
name:
blank: "Please Specify a Name"
taken: "Please use a different name"
I advice you to define this way instead of customizing devise validation module
because I you follow the above approach I would be possible that you would skip a validation a place or two
for Example I the remove the above devise validation module and then substitue your own in User Model
then all the validation would work for but you would miss the validation in Change Password
There by resulting your to login even though the password was never supplied and never given
Upvotes: 6
Reputation: 4065
Those error messages belong to activerecord.
Just create a new language file with that structure and replace what you need.
activerecord:
errors:
messages:
confirmation: "and Password must match."
You can define own errors for models or model attributes. The values :model, :attribute and :value are always available for interpolation.
For example,
models:
user:
blank: "This is a custom blank message for %{model}: %{attribute}"
attributes:
login:
blank: "This is a custom blank message for User login"
Please read more on this link depending on your requirement https://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml
Upvotes: 6