# How to validate multi-option checkboxes

**URL:** <https://www.ruby-forum.com/t/how-to-validate-multi-option-checkboxes/81762>\
**Category:** Rails\
**Created:** [January 13, 2007, 1:46am UTC](https://www.ruby-forum.com/t/how-to-validate-multi-option-checkboxes/81762 "2007-01-13T01:46:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jesse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jesse](https://www.ruby-forum.com/u/jesse)\
**Post date:** [January 13, 2007, 1:46am UTC](https://www.ruby-forum.com/t/how-to-validate-multi-option-checkboxes/81762/1 "2007-01-13T01:46:44Z")

</div>

Hi,

I am trying to create a form for a dating site where users can specify  
the body types they seek in their ideal match (ex: Slender, slim,  
average, fat, etc.). Since users may be attracted to more than one body  
type, they are allowed to check multiple boxes.

In my SQL schema, I’ve defined this attribute as “ideal\_body\_type”. How  
should I store and validate this aggregated data in my model. Also, if  
an error occurs during validation, how do I make the checked values in  
these checkboxes stay checked?

Thanks in advance.

-Jesse

---

<div class="post-metadata">

**Author:** ![jesse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jesse](https://www.ruby-forum.com/u/jesse)\
**Post date:** [January 13, 2007, 4:09am UTC](https://www.ruby-forum.com/t/how-to-validate-multi-option-checkboxes/81762/2 "2007-01-13T04:09:34Z")

</div>

I think the easiest thing to do is to have each attribute represented as  
a column in your scheme. So instead of having one column,  
“ideal\_body\_type” have one for skinny, fat, super man, each holding a  
boolean value.

On validation, you can make sure atleast one of them is checked:

before\_save :confirm\_body\_type

def body\_type  
flag - 0  
[:skinny, :fat, :super\_man].each |body\_type|  
flag += 1 unless send(body\_type).nil?  
end  
return false if flag.zero? else true  
end

ok, so the example is cracked but the idea is somewhere in there…

Jesse wrote:

> Hi,
> 
> I am trying to create a form for a dating site where users can specify  
> the body types they seek in their ideal match (ex: Slender, slim,  
> average, fat, etc.). Since users may be attracted to more than one body  
> type, they are allowed to check multiple boxes.
> 
> In my SQL schema, I’ve defined this attribute as “ideal\_body\_type”. How  
> should I store and validate this aggregated data in my model. Also, if  
> an error occurs during validation, how do I make the checked values in  
> these checkboxes stay checked?
> 
> Thanks in advance.
> 
> -Jesse
