views:

222

answers:

1

How can I get rid of validation messages telling me that:

Login is too short (minimum is 3 characters)
Login should use only letters, numbers, spaces, and .-_@ please.
Password is too short (minimum is 4 characters)
Password confirmation is too short (minimum is 4 characters)

this happens even before map_openid_registration is called, thus not giving me any chance to fill login with something from returned registration Hash. I would like to have OpenID auto-registration (on login) without requiring user to supply login/password.

I also won't make this fields "not required" or "not validated", since I still need them with old school login/password registration. Thank you

A: 

One option would be to only validate the presence of login and password if identity_url is blank. Authlogic provides a hook for that:

openid_validation_options = { :unless => :has_openid? }
Authlogic::ActsAsAuthentic.class_eval do
  Login::Config.validates_length_of_login_field_options.merge       openid_validation_options
  Login::Config.validates_format_of_login_field_options.merge       openid_validation_options
  Password::Config.validates_length_of_password_field_options.merge openid_validation_options
  Password::Config.validates_format_of_password_field_options.merge openid_validation_options
end

class MyUserClass
  acts_as_authentic

  protected
  def has_openid?
    identity_url.present?
  end
end

Alternatively, you could set a default login and password before saving if identity_url is present:

class MyUserClass
  acts_as_authentic
  before_create :set_login_and_password_if_openid

  protected
  def set_login_and_password_if_openid
    if new_record? && identity_url.present?
      self.login ||= identity_url
      if password.blank? && password_confirmation.blank?
        self.password = self.password_confirmation = generate_random_password
      end
    end
  end

  def generate_random_password
    ...
  end
end
James A. Rosen