Rails 3 - validation of checkboxes
I have the form 2 checkboxes and I am trying to find some way, how to validate it - I want to know, if a user clicked on one of two checkboxes...
I tried to开发者_如何学C do it with validates_presence_of
, but unfortunately, without success...
Can anyone help me, please, how to do? Thank you, M.
You can use custom validate functions for this.
validate :custom_checkbox_validation
def custom_checkbox_validation
errors.add(:base, "Please check atleast one checkbox.") unless chkbox1 || chkbox2
end
To work this, You should have 2 checkboxes in your view files.
<%= form.check_box :chkbox1 %>
<%= form.check_box :chkbox2 %>
精彩评论