user4809833
user4809833

Reputation:

Customize blank errors in Devise

Now I get an error like this:

Email can't be blank

I can change just this part

can't be blank

errors:
    messages:
        blank: cannot be empty

But it after this shows as:

Email cannot be empty

Can I change field name too? make it like

E-mail_something_else cannot be empty?

How can I do it?

My field in the form:

<div class="field">
    <%= t('registration.email') %><br />
    <%= f.email_field :email, autofocus: true, :class => "form-field" %>
</div>

Upvotes: 0

Views: 1105

Answers (3)

Orkhan Alizade
Orkhan Alizade

Reputation: 7549

Try this in the devise_en.yml file:

en:
    errors:
        format: "%{message}"

The default format is "%{attribute} %{message}".

Upvotes: 1

Akshay Borade
Akshay Borade

Reputation: 2472

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"

Example => I removed the above devise validation module and then substitue your own in User Model.

Hope this will help you.

Upvotes: 2

ciaoben
ciaoben

Reputation: 3338

You can simply search for the label you need, for example if you search :already_confirmed (that is the label in the locale file), you will find that in the file:

lib/devise/models/confirmable.rb 

there is this line:

self.errors.add(:email, :already_confirmed)

If you change email in whatever key you want.

EDIT: I am searching in their github repo, so I am not user it is right, but maybe the message you need is in file:

lib/devise/models/authenticatable.rb 

in line 269

record.errors.add(key, value.present? ? error : :blank)

Upvotes: 0

Related Questions