# ? Conditional validation skipping?

**URL:** <https://www.ruby-forum.com/t/conditional-validation-skipping/123536>\
**Category:** Rails\
**Created:** [December 18, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/conditional-validation-skipping/123536 "2007-12-18T15:09:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 18, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/conditional-validation-skipping/123536/1 "2007-12-18T15:09:49Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 18, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/conditional-validation-skipping/123536/2 "2007-12-18T15:19:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 18, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/conditional-validation-skipping/123536/3 "2007-12-18T16:24:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 18, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/conditional-validation-skipping/123536/4 "2007-12-18T16:52:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 18, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/conditional-validation-skipping/123536/5 "2007-12-18T17:30:54Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 18, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/conditional-validation-skipping/123536/6 "2007-12-18T17:33:48Z")

</div>

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
