Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Showing validation errors on additional fields that are dynamically generated


  • Please log in to reply
1 reply to this topic

#1 bdensmore

bdensmore

    Passenger

  • Members
  • 2 posts

Posted 15 November 2013 - 10:01 PM

Hi all,

I've written a custom validator that is working correctly, but I'm having an issue with getting the error message to display only on the fields that are required.
 
The scenario that this validation runs on is I have a group of checkboxes(about 10 total). Some of these checkboxes have a text field next to them so that if a user selects that particular check box they need to fill out some additional text.
 
In my current scenario, there are 3 checkboxes that have an additional field that is required. If the user checks only one of them and does not fill in the required additional field, the validation is coming back and highlighting all 3 fields as having an error.
 
The way this data is stored is in model class called "Answer". If a particular answer such as this one requires additional data, there is a method named extra_input_data which will store a hash with their key/value pair. So the extra_input_data column will look like: {"1744"=>"","1743"=>"", "1742"=>""} we then have another column that will store the ID of the checkbox the user has clicked and if they click multiples we store the values pipe delimited like "123|1234" etc..
 
The validator runs on the extra_input_data and if the value stored in the answer content matches one of the ID's that is in the extra_input_data, the validator checks to make sure there isn't an empty value. That all works fine and I'll post the code below, but because these fields all use the same symbol for the name i.e. :content, :extra_input_data, I can't get the individual field to highlight.
 
In the actual HTML form the fields look like:
Checkbox:
<input checked="checked" class="check_boxes optional session-answer" id="survey_session_answer_content_1744" name="survey[session_answers_attributes][4][content][]" value="1744" type="checkbox">
Corresponding Text Field
<input class="string optional session-answer-textfield" id="survey_session_answers_4_extra_input_data_1744" maxlength="200" name="survey[session_answers_attributes][4][extra_input_data][1744]" size="50" value="test" type="text">
 
Here is my validator code:
 

class ExtraInputValidator < ActiveModel::EachValidator
 def validate_each(record, attribute, value)
  unless value.nil?
   content_arr = record.content.split('|')
   content_arr.each do |content|
        value.select{|v| v[content]}.each do |val|
         record.errors.add :content, "can't be blank" if val[1].empty?
        end
   end
  end
 end
end

Is there a way for me to highlight ONLY the field(s) that failed?

 

Thank you,

Ben



#2 bdensmore

bdensmore

    Passenger

  • Members
  • 2 posts

Posted 15 November 2013 - 10:21 PM

I'm also attaching a screen shot to show what I'm talking about.

 

Screenshot%20from%202013-11-15%2017%3A07






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users