views:

50

answers:

1

Hi, is there any possibility to perform conditional validation in authlogic's model User, that uses validation inside acts_as_authentic block depending on some conditions? I'm trying to implement multistep registration form, described in Ryan Bates's railscast 217 with authlogic.

+1  A: 

Yes, it is possible to set conditional validations with authlogic. In your User model you could have:

class User < ActiveRecord::Base

acts_as_authentic do |config|
  config.merge_validates_format_of_login_field_options {:if => :do_validation}
end

where :do_validation is a method in User that returns true or false based on some condition you define. There are other hooks you can use as well. See http://www.rubydox.net/class/authlogic/2.1.3/Authlogic::ActsAsAuthentic::Login::Config for info related to this example. You can do similar things for password field.

Please note the example code above is from memory as I am not in a place where I can refer to my own source in which I did this very thing. I will verify tomorrow when I return to work and correct if necessary.

UPDATE I verified my code example is correct. It may be useful to also note that any of the standard RoR options for validates_format_of can be merged into this particular authlogic config setting. http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001401

Warren