Rails & Devise: Email Confirmation text field?

Viewed 3837

I am using Rails 3.2 and Devise. I am wondering if Devise provides an "Email (address) Confirmation" field just like Password Confirmation for the user to type in a Password field and then type in a Password Confirmation field before the website sends out a confirmation email or processes the sign up registration. If not, must I add a email_confirmation and validate the User model (after rails g devise User)?

Thanks in advance

4 Answers

I was faced with the same issue.

Here's how I solved it

From the official RailsGuide

You should use this helper when you have two text fields that should receive exactly the same content. For example, you may want to confirm an email address or a password. This validation creates a virtual attribute whose name is the name of the field that has to be confirmed with "_confirmation" appended.

class Person < ApplicationRecord
  validates :email, confirmation: true
end

In your view template you could use something like

<%= text_field :person, :email %>
<%= text_field :person, :email_confirmation %>

This check is performed only if email_confirmation is not nil. To require confirmation, make sure to add a presence check for the confirmation attribute (we'll take a look at presence later on in this guide):

class Person < ApplicationRecord
  validates :email, confirmation: true
  validates :email_confirmation, presence: true
end

There is also a :case_sensitive option that you can use to define whether the confirmation constraint will be case sensitive or not. This option defaults to true.

class Person < ApplicationRecord
  validates :email, confirmation: { case_sensitive: false }
end

The default error message for this helper is "doesn't match confirmation".

From my experimentation, using the official RailsGuide,

If you want to change the confirmation message to say, Email confirmation must be given correctly, then use add a message option this way:

class Person < ApplicationRecord
  validates :email, confirmation: true
  validates :email_confirmation, presence: { message: "must be given correctly" }
end

That's all.

I hope this helps

There are a few minor differences for newer versions of Rails compared to the original answer. Just figured I'd add this in case it helps anyone else.

Add an attr_accessor for email_confirmation to your model and validate the email field against the email_confirmation field. Set case_sensitive to false { case_sensitive: false } on the validation unless you have configured the email field to be case sensitive.

app/models/user.rb

class User < ApplicationRecord
  attr_accessor :email_confirmation
  validates :email, confirmation: { case_sensitive: false }
end

Permit email_confirmation on the sign up params.

app/controllers/users/registrations_controller.rb

def configure_sign_up_params
  devise_parameter_sanitizer.permit(:sign_up, keys: [:email_confirmation])
end

Add the email_confirmation field to your sign up form.

app/views/devise/registrations/new.html.erb

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= render 'devise/shared/error_messages', resource: resource %>

  <div class="field">
    <%= f.label :email %>
    <%= f.email_field :email, autofocus: true, autocomplete: 'email', required: true %>
  </div>

  <div class="field">
    <%= f.label :email_confirmation %>
    <%= f.email_field :email_confirmation, required: true %>
  </div>

  <!-- other fields -->
<% end %>
Related