mardi 26 janvier 2016

Validating a radio button and checkbox in rails model

Overview

In my view I have two radio_button form helpers: Yes and No

I also have one check_box with the identifier :agreement

Here is my code:

%h6 Is this a membership?   

= f.radio_button :agreement, true, id: "membership_yes" 

= f.label :agreement, "Yes", value: true

= f.radio_button :agreement, false, id: "membership_no" 

= f.label :agreement, "No", value: false

%br/ 

= f.check_box :agreement, type: "checkbox", id: "agreement-box"
= f.label :agreement, label: "I acknowledge that I have completed at least 8 workouts for the month."

What I want to accomplish

In my rails model I would like to perform validations on the :agreement checkbox.

If Yes is selected then show the checkbox and make it required. If No is selected then hide the checkbox and make it not required.

I would greatly appreciate any help or resources that would point me in the right direction.

What I have tried

I have tried giving the radio buttons and checkbox id's while using Jquery to hide, show, and set the required property of the checkbox to false or true.

I could only show and hide the checkbox. When setting the required property to true or false it was ignored and the form submitted no matter what when I pressed the Submit button

If there are any better ways to organize my code I would appreciate the suggestions. Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire