client_side_validations icon indicating copy to clipboard operation
client_side_validations copied to clipboard

Current password can't be blank - on user edit form (using devise)

Open richardjortega opened this issue 14 years ago • 11 comments

I'm getting the following error after the user tries to update their information, it returns as "Current password can't be blank".

I've followed the steps in the wiki on devise as well as checking out other issues so didn't know where to post this, thanks.

Here is the supporting information:

gem 'client_side_validations', :git => 'git://github.com/bcardarella/client_side_validations.git' 
gem 'rails', '3.2.8'
gem 'devise', '2.1.2'

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :email_regexp => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

  # Setup accessible (or protected) attributes for your model
  attr_accessible :first_name,
                  :last_name, 
                  :email, 
                  :password, 
                  :password_confirmation, 
                  :remember_me, 
                  :stripe_token,
                  :phone_number,
                  :location,
                  :locations_attributes

  validates_presence_of :email
  validates_length_of :password, :minimum => 6, :on => :create
  validates_uniqueness_of :email, :case_sensitive => false

  validates_presence_of :password, :on => :create
  validates_confirmation_of :password

devise/registrations/edit.html.erb template

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :class => 'form-horizontal', :method => :put }) do |user_form| %>

  <%= devise_error_messages! %>

 <div class="control-group">
    <label class="control-label">Password</label>
    <div class="controls">
      <%= user_form.password_field :password, :validate => { :presence => true, :format => true } %>
      <span class="help-block">Leave blank if you don&rsquo;t want to change it</span>
    </div>
  </div>
  <div class="control-group password-two">
    <label class="control-label">Retype Password</label>
    <div class="controls">
      <%= user_form.password_field :password_confirmation %>
    </div>
  </div>

rendered HTML

<form accept-charset="UTF-8" action="/users" class="form-horizontal" data-validate="true" id="edit_user" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="udpfFuP9s8QV/tP2UrhFvcqNXSB331qdSFpxviYhu+Y=" /></div>

<div class="control-group">
    <label class="control-label">Password</label>
        <div class="controls">
            <input id="user_password" name="user[password]" size="30" type="password" />
            <span class="help-block">Leave blank if you don&rsquo;t want to change it</span>
        </div>
</div>

<div class="control-group password-two">
    <label class="control-label">Retype Password</label>
        <div class="controls">
            <input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password" />
        </div>
</div>

richardjortega avatar Oct 17 '12 18:10 richardjortega

@richardjortega have you been through the Using Devise wiki page yet? https://github.com/bcardarella/client_side_validations/wiki/Using-Devise

bcardarella avatar Oct 17 '12 19:10 bcardarella

@bcardarella I have gone through both Devise wiki pages and to no avail. I specifically made sure to add :validate => { :presence => true, :format => true } and on the User model add validates_confirmation_of :password at the end, in addition to adding :on => :create for validates_presence_of & validates_length_of :password

I'm kind of at a lost, any help would be greatly appreciated. Sorry to ask for support on here since I know this is for issues/tickets.

richardjortega avatar Oct 17 '12 19:10 richardjortega

No worries, let me see what I can do. I don't actually use Devise so it might take a bit for me to setup something to recreate the issue.

If you could possibly put me onto the project itself I am sure I could fix it much faster.

bcardarella avatar Oct 17 '12 19:10 bcardarella

@bcardarella wow, that's an incredible gesture, i've added you to the repo. so thankful, the branch to use is ironman

richardjortega avatar Oct 17 '12 19:10 richardjortega

@richardjortega any special instructions on getting setup?

bcardarella avatar Oct 17 '12 19:10 bcardarella

@bcardarella nothing that's out of the ordinary. a rake db:migrate and a rake db:seed should do the trick and everything should run fine locally. routes.rb has been a little tweaked from the original devise install. The main file still falls under devise default install view paths, views/devise/registrations/edit.html.erb.

Also, super awesome gem you have here. Pleasure to work with, I can only imagine the amount of Devise related items you receive.

richardjortega avatar Oct 17 '12 19:10 richardjortega

And what is the route and steps to reproduce?

bcardarella avatar Oct 17 '12 19:10 bcardarella

So I saw several problems:

  1. You should update to the latest version of the gem. The Gemfile.lock is on 3.2.6.beta.6 which is old. The latest is 3.2.0
  2. I didn't notice the rails.validations.js was being required in the pipeline. I added it in.
  3. On your User model you have a password minimum length validator. You need to add :allow_blank => true to this validator.

Please let me know if you have any additional issues.

bcardarella avatar Oct 17 '12 19:10 bcardarella

Excuse me jumping in here, but from the OP it's not clear whether this is a server-side or client-side validation error. Could you clarify?

woodyza avatar Oct 17 '12 19:10 woodyza

Thank you @bcardarella for you help, for future people having the same issue it's not a client_side_validations thing as it's definitely a Devise item I was unaware of - this link proved useful as well.

https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password

richardjortega avatar Oct 17 '12 20:10 richardjortega

For reference ( google sent me to this ticket) - here's an easy way to acomplish update with or without password:

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
     meth = params[:user][:password].blank? ? "update_without_password" : "update"

    respond_to do |format|
      if @user.send(meth, user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

sblackstone avatar Apr 09 '19 17:04 sblackstone