I'm using devise as authentication engine in my app.
Is there any way to use custom messages when devise validation fails.
Devise provides me with the following message when the password is blank: Password can't be blank, but i need another message. How can i do it?
ActiveRecord en.yml is the answer I would suggest if you want to change the Validation Message for Devise
Here is 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 if you follow the above approach, it 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
Keep a loop of that too
Cheer
Regards
Please refer to the URL below.
http://railscasts.com/episodes/210-customizing-devise?view=asciicast
If the user is signing in, you can edit all the error messages in devise.en.yml under config/locales.
If you're signing up, Devise provides its own validations out of the box without any customizing. If you want to customize it, you can edit the User model.
Find devise :validatable and remove the :validatable option. After which, you should be able to use the usual rails validations. Note that this will cause you to have to do all the validations yourself.
validates_presence_of :password, :message=>"Your custom error message!"
Some usual validations:
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :email
validates_uniqueness_of :email
Not a complete answer, but this sounds like it should be solvable with I18n, either with the devise-internal keys, or by overriding active record's validation error messages for your user model.
Here's a similar question: Devise attributes for i18n?
You can customize your devise messages from config/locales/devise.en.yml but if you want to change to validation message then delete :validatable from Model. Then you can change a validation message like before.
For example:
validates_uniqueness_of :email, :case_sensitive => false, :allow_blank => true, :if => :email_changed?
validates_format_of :email, :with => Devise.email_regexp, :allow_blank => true, :if => :email_changed?
validates_presence_of :password, :on=>:create
validates_confirmation_of :password, :on=>:create
validates_length_of :password, :within => Devise.password_length, :allow_blank => true
Related
I'm using devise and I'm trying to add an email_confirmation input, I want my users to type their email address twice in order to make sure that they didn't make any mistake (like the password_confirmation input). I have searched for a solution for days but all I can find is how to "verify" an email address. How would the validation work ? Any answer/suggestion will be greatly appreciated.
To confirm their email address, add the field email_confirmation to the form:
run rails generate devise:views so that devise views are available within the application.
add email_confirmation to the devise form.
Then allow this parameter to be passed to devise:
https://github.com/plataformatec/devise#strong-parameters
The last step is to add the validation to User model (or the model you use with devise):
validates :email, confirmation: true
You'll want to use a regular expression validation. Something like this should do the trick:
validates :email,
format: { with: /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, message: 'Invalid email' }
That's assuming that by "verify" you mean simply "check that the thing the user entered looks like a valid email".
There's an additional step of verification, not taken by most apps, which checks that the email not only looks like a valid email, but actually is an email address that exists in real life. If that's what you're after, check out https://github.com/kamilc/email_verifier
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
It seems like this should be straightforward. When signing up new users, I want custom errors for blank user names and passwords. It worked fine for the user name:
validates :name, presence: { message: "Please enter a name." },
length: { maximum: 50,
message: "Please enter a name shorter than 50 characters"}
When the field is blank, it gives the "Please enter a name." error.
I the same thing for the password:
has_secure_password
validates :password, presence: { message: "Please enter a password." },
length: { minimum: 8,
message: "Please choose a password with at least 8 \
characters."}
The minimum length message works fine. But if I submit with an empty password, I get the default "can't be blank" message.
has_secure_password automatically adds some validations for you:
Password must be present on creation
Password length should be less than or equal to 72 characters
Confirmation of password (using a password_confirmation attribute)
To prevent that, declare:
has_secure_password validations: false
And then you can add your own.
has_secure_password validations: false worked at first, but I had a problem when I made the edit user profile page. It assumes the user might not enter a password. So it depends on the validations in has_secure_password, which the answer above disables.
A better way is to edit config/local/en.yml:
en:
activerecord:
attributes:
user:
password: "Password"
errors:
models:
user:
attributes:
password:
blank: "Please enter a password."
I have a model User.
Here're a few validation fields:
validates :first_name, presence:{ message: "please add your First name"}
validates :last_name, presence:{ message: "please add your Last name"}
validates :username, presence:{ message: "please pick a username"},
When I try to sign up using devise, I have a message that I should provide first name and last name. But for registration I only want to use 3 fields shown: username, email, password.
How to skip validation of first and last names during registration?
Thanks.
You don't want the validations for first_name, last_name to run while creating a new user BUT you want them while updating an existing user record. All you need to do is , modify the validations such that they will only be run while updating a user record.
You can do this by using on: :update option as below:
validates :first_name, presence:{ message: "please add your First name"}, on: :update
validates :last_name, presence:{ message: "please add your Last name"}, on: :update
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"
In my en.yml I have this:
en:
errors:
format: "%{message}"
messages:
blank: "%{attribute} can't be blank"
invalid: "%{attribute} is invalid"
too_short: "%{attribute} is too short"
too_long: "%{attribute} is too long"
wrong_length: "%{attribute} is the wrong length"
taken: "%{attribute}, {value}, is already taken"
And here's my User model so far:
validates_presence_of :username
validates_uniqueness_of :username
validates_length_of :username, :within => 4..15
validates_format_of :username, :with => /^\w+$/i
validates_presence_of :password
validates_length_of :password, :within => 6..20
When I test random data, all error messages work great, except for the validates_uniqueness_of, which returns the default 'has already been taken'
Thank you very much in advance.
shouldn't it be
taken: "%{attribute}, %{value}, is already taken"
with percent sign for value?
I didn't know you could access value, but it makes sense, otherwise that could be username.
I see that taken is the right key, but I still would try without {value} to see if it works.
At last or temporary fix I think you can pass a message in you model validation, something like this should work:
validates_uniqueness_of :username, :mesage => "#{self.username} is already taken"
but of course you loose a lot of benefits.