# HOw do I perform this conditional validation?

**URL:** <https://www.ruby-forum.com/t/how-do-i-perform-this-conditional-validation/147977>\
**Category:** Rails\
**Created:** [September 8, 2008, 12:43am UTC](https://www.ruby-forum.com/t/how-do-i-perform-this-conditional-validation/147977 "2008-09-08T00:43:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 8, 2008, 12:43am UTC](https://www.ruby-forum.com/t/how-do-i-perform-this-conditional-validation/147977/1 "2008-09-08T00:43:33Z")

</div>

Hi,

I have this validation rule to check if the user-entered date of birth  
is at least 18 years of age …

validates\_each :date\_of\_birth do |record, attr, value|  
record.errors.add attr, “You must be at least 18 years old to  
use this site.” if value \> Date.new((Date.today.year - 18),  
(Date.today.month),(Date.today.day))  
end

Now I want to refine this so it validation only occurs if this  
condition above is true and the user\_type\_id virtual attribute is  
equal to one. How do I add this additional piece of logic into my  
validation rule?

Thanks, - Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 8, 2008, 9:24am UTC](https://www.ruby-forum.com/t/how-do-i-perform-this-conditional-validation/147977/2 "2008-09-08T09:24:24Z")

</div>

On Sep 7, 11:42 pm, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now I want to refine this so it validation only occurs if this  
> condition above is true and the user\_type\_id virtual attribute is  
> equal to one. How do I add this additional piece of logic into my  
> validation rule?

Just add record.user\_type\_id == 1 to that condition.

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 8, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/how-do-i-perform-this-conditional-validation/147977/3 "2008-09-08T16:16:50Z")

</div>

I guess I’m slow. How would that look with the code above? -

On Sep 8, 1:23 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 8, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/how-do-i-perform-this-conditional-validation/147977/4 "2008-09-08T16:20:21Z")

</div>

On 8 Sep 2008, at 15:05, removed\_email\_address@domain.invalid wrote:

> I guess I’m slow. How would that look with the code above? -

just add it to the if you’ve got.

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 8, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/how-do-i-perform-this-conditional-validation/147977/5 "2008-09-08T21:50:37Z")

</div>

Thanks. I think I’m still doing something wrong because I now have

validates\_each :date\_of\_birth do |record, attr, value|  
record.errors.add attr, “You must be at least 18 years old to  
use this site.” if :user\_type\_id != 1 && value \>  
Date.new((Date.today.year - 18),(Date.today.month),(Date.today.day))  
end

but I’m getting an error upon trying to create my object. The error  
is

NoMethodError in Super admin/subscriberController#create  
You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.\>

app/models/user.rb:10  
app/controllers/super\_admin/subscriber\_controller.rb:43:in `create’

Request

Parameters:

{“user”=\>{“pharmacy\_id”=\>“333”,  
“work\_phone\_extension”=\>“”,  
“credit\_card\_number”=\>“”,  
“user\_type\_id”=\>“1”,  
“ship\_to\_last\_name”=\>“Alvarado”,  
“ship\_to\_city”=\>“Denver”,  
“credit\_card\_expiration\_date(1i)”=\>“2008”,  
“credit\_card\_expiration\_date(2i)”=\>“9”,  
“ship\_to\_country”=\>“US”,  
“password\_confirmation”=\>“password”,  
“credit\_card\_security\_code”=\>“”,  
“ship\_to\_street”=\>“1201 Galapago “,  
“ship\_to\_zip”=\>“80204”,  
“credit\_card\_expiration\_date(3i)”=\>“8”,  
“bill\_to\_city”=\>””,  
“credit\_card\_company\_id”=\>“1”,  
“phone”=\>“512-222-3333”,  
“fax”=\>“”,  
“bill\_to\_last\_name”=\>“”,  
“bill\_to\_street”=\>“”,  
“ship\_to\_first\_name”=\>“Dave”,  
“ship\_to\_state”=\>“CO”,  
“login”=\>“bigpharma”,  
“password”=\>“password”,  
“bill\_to\_state”=\>“”,  
“ship\_to\_company”=\>“Dave Pharm”,  
“email”=\>“removed\_email\_address@domain.invalid”,  
“bill\_to\_first\_name”=\>“”,  
“bill\_to\_zip”=\>“”},  
“commit”=\>“Create”}

On Sep 8, 8:19 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
