Tatva-Artha

meaning of "it"

Authlogic: customizing default validations

with one comment

On new Ruby/Rails projects, we are going with authlogic for authentication needs.

If you’ve worked with authlogic, you know that adding acts_as_authentic to your User/Account model loads it with lots of useful functionality like database creation/update with username, email, password and all validations etc. The validation constraints are preety sensible however our project needed to tweak those a little.

Here’s a couple of gotchas I ran into when customizing those:

Making email optional:
Since we weren’t using email as the username for logging in, email weren’t required in our system. The default email format validation makes email required in authlogic. I had to add following cusomizations to make email optional.

  acts_as_authentic do |config|
    # make email not required (authlogic requires it by default)
    config.merge_validates_length_of_email_field_options :allow_nil => true
    config.merge_validates_format_of_email_field_options :allow_nil => true
  end

Note: I needed to make it optional on both lengthvalidation and format-validation.

Next,

Change allowable email length:
The default is 6-100. We wanted 6-255. So, I did:

  acts_as_authentic do |config|
    # make email not required (authlogic requires it by default)
    config.merge_validates_length_of_email_field_options :in => 6..255, :allow_nil => true
    config.merge_validates_format_of_email_field_options :allow_nil => true
  end

It failed with:

/../lib/active_record/validations.rb:572:in `validates_length_of': Too many range options specified.  Choose only one. (ArgumentError)

It turned out authlogic uses :within and :in is an alias of :within. Easy, I replaced :in with :within.

Next, Customizing password length
The default for authlogic is minimum of 6 characters for password. Authlogic, understandibly, does not impose upper limit on password length. We needed length in rante 8-24.

So, I did:

    config.merge_validates_length_of_password_field_options :within => 8..24

This failed with “too many range options”. Since, authlogic uses :minimum => 6, I tried following:

    config.merge_validates_length_of_password_field_options :minimum =>8, :maximum =>24

Surprisingly, it still failed with “too many range options”. It turned out that ActiveRecord only allows either :minimum or :maximum. If you have both, you should use :within or :in. So, I needed to remove the :minimum option (default in authlogic and instead add :within). Luckily, authlogic allows completely over-writing options. So, I did:

  acts_as_authentic do |config|
    password_length_constraints = config.validates_length_of_password_field_options.reject { |k,v| [:minimum, :maximum].include?(k) }
    config.validates_length_of_password_field_options = password_length_constraints.merge :within => 8..24
  end

Of course, I could have just simply set the options to what I needed. But this allow me to just selectively delete fix options while keeping others that authlogic may need (like :if => require_password? for password).

Good stuff!

http://www.tatvartha.com/wp-content/plugins/sociofluid/images/digg_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/reddit_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/stumbleupon_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/delicious_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/google_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/twitter_16.png

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Written by Sharad

August 5th, 2009 at 6:11 pm

Posted in All

One Response to 'Authlogic: customizing default validations'

Subscribe to comments with RSS or TrackBack to 'Authlogic: customizing default validations'.

  1. Hi,

    thanks for the detailed writeup! You can also do:

    config.validate_email_field false

    to switch off all the email validation checks.

    Cheers
    Dave

    Dave Hollingworth

    26 Jan 10 at 5:58 pm

Leave a Reply