Devise Custom Messages When Validation Fails

Devise custom messages when validation fails

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

How to change Validation error messages in devise in Rails 5

You'll have to override the devise.en.yml file in the following manner

en:
activerecord:
errors:
models:
user:
attributes:
password:
too_short: "Password is too short (minimum is %{count} characters)"

Devise Custom Error Messages

Try it:

en:
mongoid:
attributes:
user:
email: 'Your name for email'

'user' is the model and 'email' the field you want to translate.

Devise activerecord errors custom messages and its translation

"Why is that? Why does it show 'Email' word at the beginning of the translation"

That's how the full_messages method work by default (default format is "%{attribute} %{message}").

You can change that with any of this I18n keys:

activemodel.errors.models.user.attributes.email.format
activemodel.errors.models.user.format
errors.format

Check the docs for more information: https://apidock.com/rails/v6.0.0/ActiveModel/Errors/full_message

EDIT: something like this on your yml file:

pt:
activemodel:
errors:
user:
attributes:
email:
blank: 'bla bla bla'
format: '%{message}'

Working with Devise Login / Session Validation Error Messages in Rails 5.0

The Devise help recommends that you include flash messages in your application layout file. Yours doesn't appear to have these; try including them. This will enable flash messages across the application, regardless of what route is being hit.

For a basic Rails project, you might use something like this:

<% if flash[:notice].present? %>
<p><%= flash[:notice] %></p>
<% end %>

<% if flash[:alert].present? %>
<p><%= flash[:alert] %></p>
<% end %>

If you're using Bootstrap with Glyphicons, I find the following template works quite well:

<% if flash[:notice].present? %>
<div class="alert alert-info">
<span class="glyphicon glyphicon-info-sign"></span>
<%= flash[:notice] %>
</div>
<% end %>

<% if flash[:alert].present? %>
<div class="alert alert-danger">
<span class="glyphicon glyphicon-exclamation-sign"></span>
<%= flash[:alert] %>
</div>
<% end %>


Related Topics



Leave a reply



Submit