Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authlogic: generates "Email has already been taken" error twice

I'm using authlogic with my user model, with the login field set to use email, thus:

acts_as_authentic do |c| 
  c.login_field = :email 
end  

If i try to make a new user, and the email is already in use, it adds a duplicate email error twice: (from the console)

user = User.new(:first_name => "fred", :last_name => "Smith", :email => User.last.email);user.valid?;errors = user.errors
=> {:email=>["has already been taken", "has already been taken"]} 

I'm guessing this is something to do with using the email as the login, maybe? I don't have any other validations on email apart from validates_format_of, and i experimented with removing that validation (doesn't make any difference).

It's a pain as it's messing up my the errors i show on the form when validation fails.

Before i go trying to hack authlogic, does anyone know why this might be happening? thanks, max

like image 282
Max Williams Avatar asked Jun 29 '11 12:06

Max Williams


2 Answers

I just ran into the same issue. Try calling config.validate_email_field = false in the acts_as_authentic block. It'll still validate the login field, which in our case in the email field, but only once.

acts_as_authentic do |config|
  config.login_field :email
  config.validate_email_field = false
    ...
end
like image 54
ccurtisj Avatar answered Sep 28 '22 00:09

ccurtisj


There are two methods in Authlogic: validate_email_field and validate_login_field.

These methods disable/enable the specific validations:

  • validate_email_field: https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/acts_as_authentic/email.rb#L101-L103
  • validate_login_field: https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/acts_as_authentic/login.rb#L132-L134

I also use config.login_field :email in my app, and I need to be sure that User#email is validated. So in my case I chose to use config.validate_login_field = false because of the difference between the validations.

like image 20
Maxim Avatar answered Sep 28 '22 00:09

Maxim