How to validate checkboxes

have the following in a partial
<% OrganisationType.find_all_by_live(true).each do |ot| %>
<%= check_box_tag “organisation[‘organisation_type_ids’][]”, ot.id,
@organisation.have_org_type(ot.id, @organisation.id) %>
<%= ot.name %>
<% end %>

in the Organisation model I would like to validate that at least one
of the checkboxes is checked. I am sure this is simple, but, er, I am
simpler …
Any one able to point me in the right direction?

many thanks
Ben

BenR wrote:

have the following in a partial
<% OrganisationType.find_all_by_live(true).each do |ot| %>
<%= check_box_tag “organisation[‘organisation_type_ids’][]”, ot.id,
@organisation.have_org_type(ot.id, @organisation.id) %>
<%= ot.name %>
<% end %>

in the Organisation model I would like to validate that at least one
of the checkboxes is checked. I am sure this is simple, but, er, I am
simpler …
Any one able to point me in the right direction?

many thanks
Ben

If you have just one check box then probably you could have used
validates_acceptance_of.

Since you have multiple, I think you need to manually loop through
before save

Rails L. wrote:

BenR wrote:

have the following in a partial
<% OrganisationType.find_all_by_live(true).each do |ot| %>
<%= check_box_tag “organisation[‘organisation_type_ids’][]”, ot.id,
@organisation.have_org_type(ot.id, @organisation.id) %>
<%= ot.name %>
<% end %>

in the Organisation model I would like to validate that at least one
of the checkboxes is checked. I am sure this is simple, but, er, I am
simpler …
Any one able to point me in the right direction?

many thanks
Ben

Ben, In your model, you can create a method something like

#your model

validate :one_must_be_checked

def one_must_be_checked
unless chkbox1 || chkbox2 || chkbox3
errors.add_to_base “One of the Checkboxes must be accpeted”
end
end

Let me know how it goes…

http://www.classifiedscript.in
Craigslist classifieds clone in Rails

Wow. Just wow.
I am seriously impressed with the response. Thanks guys - I am new to
RoR (a PHP guy before) and finding this brave new world a strange and
mysterious place. On the whole I love it, but I am still in that
floundering around hoping for the clouds of obscurity to part, stage.
Will try this out and let you know.
Again - thanks

Ben