Sunday, March 4, 2018

YesNoWidget(CheckBoxInput) widget woes

In some training software a student can use 'Yes', 'No' or any other string as valid answers for up to six questions in a set and all of them have to be answered correctly to get a score. When checking student answers against the set answers, we ignore case and punctuation and leading zeroes if the answers are numeric.

I want to use a checkbox widget if all the answers (up to six) are 'Yes' or 'No' . If any of the answers don't fit the mould we use an ordinary input field.

I cannot get the following widget working and wonder if someone can help?
 
class YesNoWidget(CheckboxInput):      """ from https://docs.djangoproject.com/en/1.11/_modules/django/forms/widgets/#CheckboxInput """        def value_from_datadict(self, data, files, name):          #print('29 forms.py self= %s\n Name=%s\n data=%s' % (self, name, data))            if name not in data:              # A missing value means False because HTML form submission does not              # send results for unselected checkboxes.              return False          value = data.get(name)          # Translate true and false strings to boolean values.          values = {'true': True, 'false': False}          if isinstance(value, six.string_types):              value = values.get(value.lower(), value)          if bool(value) == True:              return 'Yes'          return ''        def value_omitted_from_data(self, data, files, name):          # HTML checkboxes don't appear in POST data if not checked, so it's          # never known if the value is actually omitted.            return ''    
This visibly renders properly and returns 'Yes' when the checkbox is checked but triggers errors (see below) which I don't want.

[05/Mar/2018 15:53:32] "POST /question/115/ HTTP/1.1" 200 3739  <tr><th><label for="id_answer_a">Answer a:</label></th><td><ul class="errorlist"><li>Select a valid choice. Yes is not one of the available choices.</li></ul><input type="checkbox" name="answer_a" value="Yes" id="id_answer_a" checked /></td></tr>  <tr><th><label for="id_answer_b">Answer b:</label></th><td><ul class="errorlist"><li>Select a valid choice. Yes is not one of the available choices.</li></ul><input type="checkbox" name="answer_b" value="Yes" id="id_answer_b" checked /></td></tr>  <tr><th><label for="id_answer_c">Answer c:</label></th><td><ul class="errorlist"><li>Select a valid choice. False is not one of the available choices.</li></ul><input type="checkbox" name="answer_c" id="id_answer_c" /></td></tr>  <tr><th><label for="id_answer_d">D:</label></th><td><input type="text" name="answer_d" maxlength="32" id="id_answer_d" /></td></tr>  <tr><th><label for="id_answer_e">E:</label></th><td><input type="text" name="answer_e" maxlength="32" id="id_answer_e" /></td></tr>  <tr><th><label for="id_answer_f">F:</label></th><td><input type="text" name="answer_f" maxlength="32" id="id_answer_f" /></td></tr>
The correct answers for the first two are 'Yes' or 'yes' or 'y' the correct answer for the third is 'No', 'no', 'n' or ''

I understand that unchecked checkboxes are omitted from the POST data and get converted to False. My mission is to convert them to nothing or 'No' and to make my valid choices valid so we don't get an errorlist.

Thanks for any ideas

Mike

No comments:

Post a Comment