Model Validation - 2 check boxes must be selected in a form

3 views
Skip to first unread message

Chris Arndt

unread,
Oct 21, 2011, 10:49:39 AM10/21/11
to mad-r...@googlegroups.com
Hi all,

I have a form that has 21 check boxes and a user must select two of them in order for the form to submit.  In my model how do I check to see if only two of the Majors were selected?

I originally created a separate table to hold all of the Majors but I was having difficulty in including a second table in the form and finally just added the Majors to the first table.  I am curious if you would have created two tables or just one for this form?  The CALS Major check boxes have a type of boolean.

I have enclosed a mock-up of the form if that helps any and my attempts at trying to do this validation are in model.txt file.

Thanks for any help you can offer.
Chris Arndt
model.txt
Mockup-VisitDayForm.doc

Steven Faulkner

unread,
Oct 21, 2011, 11:41:05 AM10/21/11
to mad-r...@googlegroups.com
From a UX perspective, I wouldn't use 21 checkboxes.  Having that many will only lead to confusion for the end user.  As I understand you need two and only two checked?  Why not have two drop down select boxes? Each box contains all 21 choices.  You will still run into the case where people will select two of the same, so its not fool proof.  You could run some javascript to make sure that they don't select the same one for both or in rails validate uniqueness within the scope of the registration form.

If your convinced you need 21 check boxes, you could check to make sure they select only 2 using some JS as well.  Something like here: http://stackoverflow.com/questions/4135210/html-multiselect-limit

Now lets talk ruby.  If you want to be flexible (maybe your boss next week says "we want to have them select 4 majors"), I think you actually need 3 tables:

class Registration
  has_many :registration_majors
  has_many :majors, :through => :registration_majors

class Major
  has_many :registration_majors
  has_many :registrations, :through => :registration_majors

class RegistrationMajor
  belongs_to :major
  belongs_to :registration

Then you can use accepts_nested_attributes_for in combination with a nested form.  See the railscasts on this here: http://railscasts.com/episodes/196-nested-model-form-part-1

-Steve


--
You received this message because you are subscribed to the Google Groups "Mad Railers, the Madison, WI Ruby on Rails Users Group" group.
To visit the Madison Rails Home page, go to http://madisonrails.com/
To post to this group, send email to Mad-R...@googlegroups.com
To unsubscribe from this group, send email to Mad-Railers...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Mad-Railers?hl=en

Chris Arndt

unread,
Oct 21, 2011, 12:46:49 PM10/21/11
to mad-r...@googlegroups.com
Thanks Steve!

We are going with the two drop down select boxes that contains all choices.

Thanks for explaining about the tables.  I know that I will need to tackle 'using two tables' sooner or later and will definitely check out that Rails cast. My first attempts at trying to use more than one table wasn't working but I didn't follow the rails cast, just looked at the code below it ... will definitely sit down and go through the rails cast.
~Chris
Reply all
Reply to author
Forward
0 new messages