2

I'm using authlogic with rails 3. I have this in my user model:

 validates :last_name, :presence => true

  acts_as_authentic do |c|
    c.validates_length_of_password_field_options = {:minimum => 7}
  end

And then I have a controller action that updates the user's name:

  def update_name
    if @current_user.update_attributes(params[:user])
      flash[:success_name] = true
      redirect_to edit_user_via_account_settings_path
    else
      render 'edit_user_via_account_settings'
    end
  end

If the user enters a blank last name and attempts to update their name with this controller action, the @current_user model correctly has errors on last name, but it also has errors on password (password must be a minimum of 7 chars). How can I only validate the password if the password is being updated?

James
  • 5,273
  • 10
  • 51
  • 76

2 Answers2

2

You need to use the merge_validates_* config methods instead of the validates_* methods. The former keeps the conditionals (like ignore blank password) and the latter overwrites them. That should clear everything up. And don't use the assignment on the merge_* methods.

a.merge_validates_length_of_password_field_options :minimum => 7 
Syed Aslam
  • 8,707
  • 5
  • 40
  • 54
1

I think the config you are looking for is found here

http://rdoc.info/github/binarylogic/authlogic/master/Authlogic/ActsAsAuthentic/Password/Config#ignore_blank_passwords-instance_method

Unless its a new user, if no password is supplied it does not validate the password.

Rabbott
  • 4,282
  • 1
  • 30
  • 53