Re: Validations on array checkbox in Model

Frederick Cheung
Fri, 07 Nov 2008 12:59:37 -0000

On 7 Nov 2008, at 12:50, babar wrote:

> 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
%>
>  <% 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


That''s not how you could do it. You don''t validate against
params[:sub_ids], you validate against a property of the model (ie
subjects.size)

Fred


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---