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...
> 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
> On nov. 30, 13:44, ChrisRichards <evilgeen...@gmail.com> wrote:
> > 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