I have validation that I need to be able to bypass depending on a cookie
that the user might have.
My problem is for most users, they have to have a .edu or .com email
address. So I wrote that into the validation.
But if a certain cookie is set, I want to allow them to register with
any email address, and still run the other validations (uniqueness of
email, etc.)
Does anyone have any information on how to do this?
USER MODEL
validates_presence_of :first_name, :last_name, :email, :password
validates_uniqueness_of :email
validates_format_of :email,
:with => %r{.(edu|com)$}i,
:message => “Must be a valid email address”
Cookies are controller only. You will somehow have the let the model
know that it’s ok to skip the validation (eg set an attribute).
Fred
Hmm, so there is no way to check for a cookie in the validation? That
would make things so much easier…
Is there a way to check for the cookie in the controller and then pass
through a parameter so that certain validation is skipped? Something
like this in the controller, but make it only skip certain validation
instead of all:
if cookies[:is_admin] == “YES”
user.save(false)
else
user.save
end
through a parameter so that certain validation is skipped? Something
like this in the controller, but make it only skip certain validation
instead of all:
I’ve done similar things with something like
class User
attr_accessor :extended_signup
validates … :if => :extended_signup
end
at this point
user.extended_signup = true
user.save
runs the validation
user.extended_signup = false
user.save
doesn’t
Fred
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.