Validate fields (password, login) depending on the type of user

23 views
Skip to first unread message

ChrisRichards

unread,
Nov 30, 2010, 8:44:18 AM11/30/10
to Authlogic
Hi

I am using the same user model for all the different types of users in
my rails app. Most of the time I want to validate the login and
password details but in some other situations I am not using the login
and password fields so I don't want to validate them.

Is there a way to turn validations off in certain circumstances?

Thanks
Chris

gezope

unread,
Dec 1, 2010, 7:39:55 AM12/1/10
to Authlogic
Hello Chris,

you usually do your validates in your model, here is a good guide for
it:
http://guides.rubyonrails.org/active_record_validations_callbacks.html

So many types of validations, like validates_presence_of or
validates_numerilacity_of, etc. and you can also write your own
validations if you want.

If you want to turn it of maybe it's a good idea to give an if clause
after it, to ensure those circumstances are exists.

I also found this:
http://stackoverflow.com/questions/279478/how-can-i-disable-a-validation-and-callbacks-in-a-rails-sti-derived-model

You want to turn off validations in general? May I ask why?

I'm interested in your solution, it seems an interesting issue, please
let me know.
good luck,
Zoltan

ChrisRichards

unread,
Dec 2, 2010, 8:01:47 AM12/2/10
to Authlogic
Hi Zoltan

> You want to turn off validations in general? May I ask why?

I only want to turn off validations for one type of user, which are
the survey participants. The survey participants do not log in with a
username and password, they are sent a link in an email, like this :

http://site.com/login/jjk89ykjh9y9y9h9yy9y89121

Where jjk89ykjh9y9y9h9yy9y89121 is a UUID.
So their user accounts do not have a login or password.

This is my solution

class User < ActiveRecord::Base

ADMIN = "admin"
SITE_ADMIN = "site_admin"
COMPANY_USER = "company_user"
SITE_TRANSLATOR = "site_translator"
ANSWER_TRANSLATOR = "answer_translator"
PARTICIPANT = "participant"
ROLES = [ADMIN, SITE_ADMIN, COMPANY_USER, SITE_TRANSLATOR,
ANSWER_TRANSLATOR, PARTICIPANT]

acts_as_authentic do |c|
#only validate password if the user is the correct type of user.
#Participant users don't use login/password

c.merge_validates_format_of_login_field_options :if=>:requires_login_details

c.merge_validates_length_of_login_field_options :if=>:requires_login_details

c.merge_validates_confirmation_of_password_field_options :if=>:validate_password?

c.merge_validates_length_of_password_confirmation_field_options :if=>:validate_password?

c.merge_validates_length_of_password_field_options :if=>:validate_password?
end

private

def validate_password?
new_record? ? requires_login_details : password.present? ||
password_confirmation.present?
end

#determine if user needs to have login/password
#& = array intersection method
def requires_login_details
(roles & [ADMIN, SITE_ADMIN, COMPANY_USER]).any?
end

def roles
(user_roles||"").split(" ")
end

On Dec 1, 12:39 pm, gezope <gez...@gmail.com> wrote:
> Hello Chris,
>
> you usually do your validates in your model, here is a good guide for
> it:http://guides.rubyonrails.org/active_record_validations_callbacks.html
>
> So many types of validations, like validates_presence_of or
> validates_numerilacity_of, etc. and you can also write your own
> validations if you want.
>
> If you want to turn it of maybe it's a good idea to give an if clause
> after it, to ensure those circumstances are exists.
>
> I also found this:http://stackoverflow.com/questions/279478/how-can-i-disable-a-validat...
Reply all
Reply to author
Forward
0 new messages