This uses jQuery to make it possible to enable and disable fields in the
default forms, by specifying an attribute on the form class. The form
class still has to implement proper validators, so it does not rely on
client side validations.
'formitemtype': instance._meta.verbose_name,
'markdownfields': markdownfields,
'form_intro': hasattr(form, 'form_intro') and form.form_intro or None,
+ 'toggle_fields': hasattr(form, 'toggle_fields') and form.toggle_fields or None,
'savebutton': (itemid == "new") and "New" or "Save",
'operation': (itemid == "new") and "New" or "Edit",
}, NavContext(request, navsection))
{%endfor%}
}
</script>
+{%if toggle_fields%}
+<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js"></script>
+<script language="javascript">
+{%for f in toggle_fields%}
+function toggle_{{f.name}}() {
+ v = $('#id_{{f.name}}').prop('checked');
+{%for c in f.fields%}
+{%if f.invert%}
+ if (v) {
+{%else%}
+ if (!v) {
+{%endif%}
+ $('#id_{{c}}').attr('disabled', true);
+ } else {
+ $('#id_{{c}}').removeAttr('disabled');
+ }
+{%endfor%}
+}
+$(document).ready(function() {
+ $('#id_{{f.name}}').change(function() {
+ toggle_{{f.name}}();
+ });
+ toggle_{{f.name}}();
+});
+{%endfor%}
+</script>
+{%endif%}
{%endif%}
{%endblock%}