# How To Validate Boolean Form Field

**URL:** <https://www.ruby-forum.com/t/how-to-validate-boolean-form-field/74984>\
**Category:** Rails\
**Created:** [March 1, 2006, 12:44am UTC](https://www.ruby-forum.com/t/how-to-validate-boolean-form-field/74984 "2006-03-01T00:44:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [March 1, 2006, 12:44am UTC](https://www.ruby-forum.com/t/how-to-validate-boolean-form-field/74984/1 "2006-03-01T00:44:42Z")

</div>

Hello all.

I’ve got the following in my model:

class Individual \< ActiveRecord::Base  
validates\_presence\_of :first\_name, :last\_name, :street, :city,  
:state, :zip  
validates\_acceptance\_of :AZ\_resident, :message =\> “You must be an  
Arizona resident.”  
end

The AZ\_resident field is present and defined as boolean (PostgreSQL  
8.1), but validation will not occur. I’ve tried adding :accept =\> “TRUE”  
and variations on the theme, to no avail.

So, what am I doing wrong? How do I validate a boolean form field? It  
may be of use to know that my view is standard scaffold-generated stuff  
at the moment.

Thanks,

-ELf

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [March 1, 2006, 1:59am UTC](https://www.ruby-forum.com/t/how-to-validate-boolean-form-field/74984/2 "2006-03-01T01:59:16Z")

</div>

On 2/28/06, Matthew F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The AZ\_resident field is present and defined as boolean (PostgreSQL  
> 8.1), but validation will not occur. I’ve tried adding :accept =\> “TRUE”  
> and variations on the theme, to no avail.
> 
> So, what am I doing wrong? How do I validate a boolean form field? It  
> may be of use to know that my view is standard scaffold-generated stuff  
> at the moment.

Have you tried this?

def validate  
errors.add(:AZ\_resident, “You must be an Arizona resident”) if  
AZ\_resident = false  
end

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [March 1, 2006, 10:57am UTC](https://www.ruby-forum.com/t/how-to-validate-boolean-form-field/74984/3 "2006-03-01T10:57:08Z")

</div>

> Have you tried this?
> 
> def validate  
> errors.add(:AZ\_resident, “You must be an Arizona resident”) if  
> AZ\_resident = false  
> end

After correcting syntax errors, I tried:

def validate  
errors.add(:AZ\_resident, “You must be an Arizona resident”) if  
:AZ\_resident == false  
end

No difference. Forms with AZ\_resident left FALSE are happily inserted  
into the database; no errors raised.

But, moreover, what is wrong with my use of the validates\_acceptance\_of  
method? According to the docs found @  
[http://rubyonrails.org/api/classes/ActiveRecord/Validations/ClassMethods.html](http://rubyonrails.org/api/classes/ActiveRecord/Validations/ClassMethods.html),  
I’m doing everything right.

Thanks folks,

-ELf

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [March 1, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-validate-boolean-form-field/74984/4 "2006-03-01T20:41:32Z")

</div>

To be able to validate a field, it must be present.

Try…  
validates\_presence\_of :first\_name, :last\_name, :street, :city,  
:state, :zip, :AZ\_resident

- Nithin

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [March 1, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/how-to-validate-boolean-form-field/74984/5 "2006-03-01T23:20:26Z")

</div>

Nithin R. wrote:

> To be able to validate a field, it must be present.

Adding :AZ\_resident to validates\_presence\_of throws a validation  
exception no matter whether True or False is selected. It’s acting as  
if there’s no value in the field.

Is this normal behavior for a Boolean form field?

Thanks,

-ELf

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [March 2, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/how-to-validate-boolean-form-field/74984/6 "2006-03-02T21:56:00Z")

</div>

or even better…

validates\_acceptance\_of :toc, {:message =\> “You must accept this  
before proceeding”, :allow\_nil =\> false}

---

<div class="post-metadata">

**Author:** ![mfeadler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@mfeadler](https://www.ruby-forum.com/u/mfeadler)\
**Post date:** [March 2, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/how-to-validate-boolean-form-field/74984/7 "2006-03-02T21:52:59Z")

</div>

You got me curious about this, so I did some more investigating. It  
seems that the behavior is broken, but you can fix it by doing  
something like this:

class Individual \< ActiveRecord::Base  
validates\_acceptance\_of :toc, :message =\> “You must accept this  
before proceeding”

def validate  
errors.add(:toc, “You must accept this before proceeding”) if  
self.toc.nil?  
end  
end

HTH
