Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove duplicate validation errors

I am having an issue with a sign up form. It was created with SimpleForm, authentication is done with Devise. When submitting the form, if the email or password is blank, it will display the error for this twice. In the user model there are presence validations for the first name, last name, profile name, password, and email. These duplicate errors only appear on the blank email and password fields. Any other blank fields will say so once.

Example:

# Errors Prohibited This User From Being Saved:

  • Email can't be blank
  • Email can't be blank
  • Password can't be blank
  • Password can't be blank

user.rb:

    class User < ActiveRecord::Base

      devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable

      attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name

      validates :first_name, :last_name, :email, :profile_name, :password, presence: true

      validates :profile_name, uniqueness: true,
                               format: {
                                  with: /^[a-zA-Z0-9_-]+$/
                               }
      has_many :posts

      def full_name
        first_name + " " + last_name
      end
    end

registrations/new.html.erb:

  <%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>

    <div class="formGroupLeft">
      <%= f.input :first_name, :input_html => { :class => 'formGroupInput'} %>
    </div>
    <div class="formGroupRight">
      <%= f.input :last_name, :input_html => { :class => 'formGroupInput'} %>
    </div>
    <div class="formGroupLeft">
      <%= f.input :email, :input_html => { :class => 'formGroupInput'} %>
    </div>
    <div class="formGroupRight">
      <%= f.input :profile_name, :input_html => { :class => 'formGroupInput'} %>
    </div>
    <div class="formGroupLeft">
      <%= f.input :password, :input_html => { :class => 'formGroupInput'} %>
    </div>
    <div class="formGroupRight">
      <%= f.input :password_confirmation, :input_html => { :class => 'formGroupInput'} %>
    </div>

    <div class="formActions">
      <%= f.button :submit, "Sign Up" %>
    </div>

  <% end %> 

Why might this be? And how can I attempt to fix it?

like image 797
anater Avatar asked Dec 16 '12 01:12

anater


1 Answers

Looks like you've specified the devise validatable plugin, which adds email/password validations.

class User
  devise :database_authenticatable, ... :validatable
end

Since you're specifying your own validations, I would omit the devise validatable plugin.

like image 100
Fiona T Avatar answered Oct 19 '22 15:10

Fiona T