# How can I selectively turn of validations

**URL:** <https://www.ruby-forum.com/t/how-can-i-selectively-turn-of-validations/128743>\
**Category:** Rails\
**Created:** [February 11, 2008, 6:37am UTC](https://www.ruby-forum.com/t/how-can-i-selectively-turn-of-validations/128743 "2008-02-11T06:37:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iqbalhyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d78d45/32.png) [@iqbalhyd](https://www.ruby-forum.com/u/iqbalhyd)\
**Post date:** [February 11, 2008, 6:37am UTC](https://www.ruby-forum.com/t/how-can-i-selectively-turn-of-validations/128743/1 "2008-02-11T06:37:56Z")

</div>

Hi,

```
   I need to selectively turn off validations for example, I want to

```

make sure that password and password\_confirmation fields are equal when  
user  
first signs up or changes his password from his profile, but not in  
other  
cases e.g. updates his profile.

```
   right now I am using a quick and dirty hack by using booleans 

```

e.g.  
if(@skip\_password\_validations != false) #–do validations --#

```
  is there anything better than this. any help would be greatly

```

appreciated.

–  
Ishaq  
[http://blogs.kahaf.com/ishaq](http://blogs.kahaf.com/ishaq)

- Programming can be fun, so can be cryptography, however they should  
not be  
combined …

---

<div class="post-metadata">

**Author:** ![iqbalhyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d78d45/32.png) [@iqbalhyd](https://www.ruby-forum.com/u/iqbalhyd)\
**Post date:** [February 11, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/how-can-i-selectively-turn-of-validations/128743/2 "2008-02-11T15:56:37Z")

</div>

On 11 Feb 2008, at 14:15, Rick DeNatale wrote:

> > cases e.g. updates his profile.  
> > validates\_presence\_of :password, :unless =\>  
> > :skip\_password\_validations?  
> > validates\_presence\_of :password\_confirmation, unless =\>  
> > :skip\_password\_validations?  
> > validates\_confirmation\_of :password, unless =\>  
> > :skip\_password\_validations?
> 
> def skip\_password\_validations?  
> @skip\_password\_validations  
> end

or even  
with\_options :unless =\> skip\_password\_validations? do  
validates\_presence\_of :password  
validates\_presence\_of :password\_confirmation  
validates\_confirmation\_of :password  
end

---

<div class="post-metadata">

**Author:** ![iqbalhyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d78d45/32.png) [@iqbalhyd](https://www.ruby-forum.com/u/iqbalhyd)\
**Post date:** [February 11, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/how-can-i-selectively-turn-of-validations/128743/3 "2008-02-11T15:16:51Z")

</div>

On 2/11/08, Muhammad I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> is there anything better than this. any help would be greatly
> 
> ```
> 
> appreciated.

Something like this:

validates\_presence\_of :password, :unless =\>  
:skip\_password\_validations?  
validates\_presence\_of :password\_confirmation, unless =\>  
:skip\_password\_validations?  
validates\_confirmation\_of :password, unless =\>  
:skip\_password\_validations?

def skip\_password\_validations?  
@skip\_password\_validations  
end

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![iqbalhyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d78d45/32.png) [@iqbalhyd](https://www.ruby-forum.com/u/iqbalhyd)\
**Post date:** [February 11, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/how-can-i-selectively-turn-of-validations/128743/4 "2008-02-11T19:59:06Z")

</div>

Thanks a lot, that really helps cleaning the code a little

On Feb 11, 2008 7:55 PM, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > make sure that password and password\_confirmation fields are equal  
> > > appreciated.  
> > > def skip\_password\_validations?
> 
> >

–  
Ishaq  
[http://blogs.kahaf.com/ishaq](http://blogs.kahaf.com/ishaq)

- Programming can be fun, so can be cryptography, however they should  
not be  
combined …

---

<div class="post-metadata">

**Author:** ![iqbalhyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d78d45/32.png) [@iqbalhyd](https://www.ruby-forum.com/u/iqbalhyd)\
**Post date:** [February 12, 2008, 5:38am UTC](https://www.ruby-forum.com/t/how-can-i-selectively-turn-of-validations/128743/5 "2008-02-12T05:38:10Z")

</div>

What about the :if parameter that validations can take?

Like this:

validates\_format\_of :password,  
:with =\> /^\w+$/,  
:message =\> ‘**Password must be alphanumeric\</  
b\>’,  
:on =\> :save  
:if =\> @skip\_password\_validations**

The parameter can even be a Proc!

---

<div class="post-metadata">

**Author:** ![iqbalhyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/d78d45/32.png) [@iqbalhyd](https://www.ruby-forum.com/u/iqbalhyd)\
**Post date:** [February 11, 2008, 4:29pm UTC](https://www.ruby-forum.com/t/how-can-i-selectively-turn-of-validations/128743/6 "2008-02-11T16:29:27Z")

</div>

Muhammad I. wrote:

> e.g.  
> if(@skip\_password\_validations != false) #–do validations --#
> 
> ```
> is there anything better than this. any help would be greatly
> 
> ```
> 
> appreciated.

What I’m doing looks like this:

validates\_format\_of :password, :with =\> /^\w+$/, :message =\> ‘ **Password must be alphanumeric** ’, :on =\> :save

validates\_length\_of :password, :minimum =\> 5, :message =\> ‘**Password to short (Must be 5 characters long)**’, :on =\>  
:create
