448 | | return '<input type="checkbox" id="%s" class="v%s" name="%s"%s />' % \ |
449 | | (self.get_id(), self.__class__.__name__, |
450 | | self.field_name, checked_html) |
| 450 | output = ['''<script> |
| 451 | function toggle_%(id)s_boolean() { |
| 452 | vis = document.getElementById('%(id)s'); |
| 453 | hidden = document.getElementById('%(id)s_hidden'); |
| 454 | if (vis.checked) { hidden.value = 'on'; |
| 455 | } else { hidden.value = 'off'; |
| 456 | } |
| 457 | return true; |
| 458 | } |
| 459 | function create_%(id)s_hidden() { |
| 460 | hidden = document.getElementById('dummy_%(id)s_hidden'); |
| 461 | hidden.innerHTML = '<input type="hidden" id="%(id)s_hidden" name="%(name)s_hidden" value="%(value)s" />'; |
| 462 | } |
| 463 | </script>''' % {'id': field_id, 'name': self.field_name, 'value': escape(htmldata) }] |
| 464 | output.append('<input type="checkbox" id="%s" class="v%s" name="%s" onClick="toggle_%s_boolean(); return true;" %s />' % \ |
| 465 | (field_id, self.__class__.__name__, |
| 466 | self.field_name, field_id, checked_html)) |
| 467 | output.append('<span id="dummy_%s_hidden"></span>' % field_id) |
| 468 | output.append('<script>create_%s_hidden();</script>' % field_id) |
| 469 | return '\n'.join(output) |
| 471 | def run_validator(self, new_data, validator): |
| 472 | """ overridden to have the right behaviour with validators when no data is posted """ |
| 473 | if new_data.has_key('%s_hidden' % self.field_name) and not new_data.has_key(self.field_name): |
| 474 | # we use internally hidden field with right value |
| 475 | new_data[self.field_name] = new_data.get('%s_hidden' % self.field_name) |
| 476 | super(CheckboxField, self).run_validator(new_data, validator) |
| 477 | |