Forum: Ruby on Rails Validation on array checkbox in Model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
393bf9ea0c286172d6920cede3a750fd?d=identicon&s=25 babar (Guest)
on 2008-11-07 14:00
(Received via mailing list)
Hi,

I am using array 'sub_id[]' for multiple checkbox,
i want to place validations on all array 'sub_ids[]' checbox in model
just
like other validations.



 <% for e in @subjects do -%>
  <input type="checkbox" name="sub_ids[]" id="<%= e.id %>" value="<%=
e.id%>"<% if @book.subjects.include? e %> checked="checked"<%end%>>
  <label for="<%= e.id %>"><%= e.name %></label>
 <% end %>



MODEL

class Book < ActiveRecord::Base
    has_and_belongs_to_many :subjects
     validates_presence_of:title
    validates_numericality_of:price
    validate :at_least_one_checkbox_was_ticked

protected
    def at_least_one_checkbox_was_ticked
    unless params[:sub_ids[]].nil?
        end
  end

  end


I'm using only two models book and subject.

thanx in adv.
This topic is locked and can not be replied to.