views:

60

answers:

1

Hi,

This is currently the setup I have:

<input id="demographics_questionary_gender_male" name="demographics_questionary[gender]" type="radio" value="Male"> Male
<input id="demographics_questionary_gender_gender" name="demographics_questionary[gender]" type="radio" value="Female"> Female
<input id="demographics_questionary_gender_male" name="demographics_questionary[gender]" type="radio" value="Other"> Other
<input id="other_gender_value" name="other_gender[value]" size="30" type="text">

This will produce three radio buttons (Male, Female, Other). There is also a text box if the "Other" radio button is selected.

My question deals with how to validate if the text box is filled in if the "Other" radio button is selected.

Currently, what I do is in the controller I check if the other radio button is selected and if it is then i'll change the stored value to the value set in the "other" text box. However, I don't really know how to validate the text box. I know how to validate if a radio button is checked by verifying that a value has been set in the model, but not how to make sure the user had inputed data into the "Other" text box.

Here is the snippet of code from the controller:

if @demographics_questionary.gender == 'Other'
    @other_gender = params[:other_gender]
    if @other_gender[:value].nil?
        @demographics_questionary.gender  = @other_gender[:value]
    else 
        @demographics_questionary.gender  = 'Not Set'
    end
 end

As you can see, I currently just set it to 'Not Set' if the 'Other' radio button is selected and the 'Other' text box is empty. However, this code is executed when a button is selected to process the data. Ideally, I would want the validation to be done at the view page where it would prevent the user from going forward until the text box is filled in if the "Other" radio button is selected. In that way, by the time it even hits the controller method this check would not be required.

But again I'm open to any other methods suggested.

I'm thinking this can be done javascript or jquery but I'm not sure how.

O yeah so all this code in the view lies in a form field:

<% form_for(@demographics_questionary) do |f| %>
  <%= f.error_messages :header_message => "Please do not skip any of the questions." %>
  /* Code for radio buttons */
  <%= f.submit 'Next' %>   

 <%end%>

Notice the radio button code is located in a form_for. once the submit button is triggered then it'll execute the method in the controller. If you were to use javascript/Jquery how would you append the function to the submit button?

This is the current page :

http://otoplusvn.com/TherapistSurvey/demographics_questionaries/new for question 2 on gender.

Any suggestion appreciate, Thanks Derek

+1  A: 

You can add dependency rules, something like this:

$('#register_form').validate({rules: {
     'other_gender[value]': {
       required: {
         depends: function(element) {
           return $("input[name=demographics_questionary[gender]]:checked").val() == "Other"
         }
       }
     }
} });

That will require the text box, only if the selected option is "Other"

Emily