? Conditional validation skipping?

Hey,

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”

On 18 Dec 2007, at 14:09, Joe P. wrote:

But if a certain cookie is set, I want to allow them to register with
:message => “Must be a valid email address”

validates_format_of :email,
:with => %r{.(edu|com)$}i,
:message => “Must be a valid email address”,
:if => :some_method

Fred

Frederick C. wrote:

On 18 Dec 2007, at 14:09, Joe P. wrote:

But if a certain cookie is set, I want to allow them to register with
:message => “Must be a valid email address”

validates_format_of :email,
:with => %r{.(edu|com)$}i,
:message => “Must be a valid email address”,
:if => :some_method

Fred

Hmm, what kind of method would work in there? I tried

validates_format_of :email,
:with => %r{.(edu|com)$}i,
:message => “Must be a valid email address”,
:if => cookies[:is_admin] != “YES”

and also have tried

validates_format_of :email,
:with => %r{.(edu|com)$}i,
:message => “Must be a valid email address”,
:if => not_admin

def not_admin
if cookies[:is_admin] == “YES”
false
else
true
end
end

But I get errors from both of them. How can I check for a cookie in the
validation code?

On 18 Dec 2007, at 15:24, Joe P. wrote:

                 :with => %r{\.(edu|com)$}i,
                 :message => "Must be a valid email address",
    :if => :some_method

Fred

Hmm, what kind of method would work in there? I tried

[snip]

But I get errors from both of them. How can I check for a cookie in
the
validation code?

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

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

On 18 Dec 2007, at 16:30, Joe P. wrote:

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