Custom error message to check if a password exists
It seems like it should be easy. When registering new users, I need custom errors for blank usernames and passwords. It worked fine for the username:
validates :name, presence: { message: "Please enter a name." },
length: { maximum: 50,
message: "Please enter a name shorter than 50 characters"}
When the field is empty, it gives "Please enter a name". mistake.
The same 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 a blank password, I get the default "cannot be blank" message.
source to share
has_secure_password validations: It worked false at first, but I got a problem when I made a user profile edit page. It is assumed that the user cannot enter a password. So it depends on the validation in has_secure_password, which disables the answer above.
Your best bet is to edit config / local / en.yml:
en:
activerecord:
attributes:
user:
password: "Password"
errors:
models:
user:
attributes:
password:
blank: "Please enter a password."
source to share
has_secure_password
automatically adds some validations for you:
Password must be present when creating
Password length must be less than or equal to 72 characters
Password confirmation (using the password_confirmation attribute)
To prevent this, declare:
has_secure_password validations: false
And then you can add your own.
source to share