_forms.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. {% load i18n %}
  2. {# Render whole form macro #}
  3. {%- macro form_widget(form, horizontal=false, width=12) -%}
  4. <fieldset class="first{% if form.fieldsets|length == 0 %} last{% endif %}">
  5. {{ form_hidden_widget(form) }}
  6. {% for fieldset in form.fieldsets %}{% if fieldset.legend %}
  7. <legend><div>{{ fieldset.legend }}{% if fieldset.help %} <span>{{ fieldset.help }}</span>{% endif %}</div></legend>{% endif %}
  8. {% for field in fieldset.fields %}
  9. {{ row_widget(field, horizontal=horizontal, width=width) }}
  10. {% endfor %}
  11. </fieldset>{% if not fieldset.last %}
  12. <fieldset{% if loop.revindex0 == 1 %} class="last"{% endif %}>{% endif %}{% endfor %}
  13. {%- endmacro -%}
  14. {# Render hidden fields macro #}
  15. {%- macro form_hidden_widget(form) -%}
  16. <input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}">{% for field in form.hidden %}
  17. <input type="hidden" name="{{ field.id }}" value="{{ field.value }}">{% endfor %}
  18. {%- endmacro -%}
  19. {# Render form row macro #}
  20. {%- macro row_widget(field, horizontal=false, width=12) -%}
  21. <div class="control-group{% if field.errors %} error{% endif %}">{% if field.label %}
  22. <label class="control-label" for="{{ field.html_id }}">{{ field.label }}:</label>{% endif %}{% if field.nested %}
  23. <div class="controls controls-nested">
  24. <div class="row">
  25. {% for subfield in field.nested %}
  26. <div class="span{{ widthratio(subfield.width, 100, width) }}">{{ field_widget(subfield, horizontal=horizontal, width=width, nested=true) }}</div>
  27. {% endfor %}
  28. </div>{% for error in field.errors %}
  29. <p class="help-block" style="font-weight: bold;">{{ error }}</p>{% endfor %}{% if field.help_text %}
  30. <p class="help-block">{{ field.help_text }}</p>{% endif %}
  31. </div>{% else %}
  32. <div class="controls">
  33. {{ field_widget(field, horizontal=horizontal, width=width) }}{% for error in field.errors %}
  34. <p class="help-block" style="font-weight: bold;">{{ error }}</p>{% endfor %}{% if field.help_text %}
  35. <p class="help-block">{{ field.help_text }}</p>{% endif %}
  36. </div>{% endif %}
  37. </div>
  38. {%- endmacro -%}
  39. {# Render form field macro #}
  40. {%- macro field_widget(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  41. {%- if field.widget == "checkbox" -%}
  42. {{ input_checkbox(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  43. {%- endif -%}
  44. {%- if field.widget == "date" -%}
  45. {{ input_date(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  46. {%- endif %}
  47. {%- if field.widget == "file_clearable" -%}
  48. {{ input_file_clearable(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  49. {%- endif -%}
  50. {%- if field.widget == "forumTos" -%}
  51. {{ input_forum_tos(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  52. {%- endif -%}
  53. {%- if field.widget == "recaptcha" -%}
  54. {{ input_recaptcha(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  55. {%- endif -%}
  56. {%- if field.widget == "radio_select" -%}
  57. {{ input_radio_select(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  58. {%- endif -%}
  59. {%- if field.widget == "select" -%}
  60. {{ input_select(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  61. {%- endif -%}
  62. {%- if field.widget == "checkbox_select_multiple" -%}
  63. {{ input_checkbox_select_multiple(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  64. {%- endif -%}
  65. {%- if field.widget == "text" -%}
  66. {{ input_text(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  67. {%- endif -%}
  68. {%- if field.widget == "textarea" -%}
  69. {{ input_textarea(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  70. {%- endif -%}
  71. {%- if field.widget == "yes_no_switch" -%}
  72. {{ input_yes_no_switch(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  73. {%- endif %}
  74. {%- endmacro -%}
  75. {# Render form field attributes macro #}
  76. {%- macro field_attrs(attrs={}, extras=[]) -%}
  77. {% for attribute in attrs %} {{ attribute }}="{{ attrs[attribute] }}"{% endfor %}{% for extra in extras %} {{ extra }}{% endfor %}
  78. {%- endmacro -%}
  79. {# Render form field class attribute macro #}
  80. {%- macro field_classes(classes=[]) -%}
  81. {% if classes %} class="{% for class in classes %}{% if not loop.first %} {% endif %}{{ class }}{% endfor %}"{% endif %}
  82. {%- endmacro -%}
  83. {# Checkbox input #}
  84. {%- macro input_checkbox(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  85. <label class="checkbox">
  86. <input type="checkbox" name="{{ field.html_name }}" id="{{ field.html_id }}" value="1"{% if field.value %} checked="checked"{% endif %}>
  87. {% if field.inline is defined %}{{ field.inline }}{% else %}{{ field.label }}{% endif %}
  88. </label>
  89. {%- endmacro -%}
  90. {# Forum Terms of Service input #}
  91. {%- macro input_forum_tos(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  92. <label class="checkbox">
  93. <input type="checkbox" name="{{ field.html_name }}" id="{{ field.html_id }}" value="1"{% if field.value %} checked="checked"{% endif %}>
  94. {% trans forum_tos=make_tos()|safe %}I have read and accept this forums {{forum_tos}}.{% endtrans %}
  95. </label>
  96. {%- endmacro -%}
  97. {%- macro make_tos() -%}
  98. <a href="{% if settings.tos_url %}{{ settings.tos_url }}{% else %}{% url 'tos' %}{% endif %}">{% if settings.tos_title %}{{ settings.tos_title }}{% else %}{% trans %}Terms of Service{% endtrans %}{% endif %}</a>
  99. {%- endmacro -%}
  100. {# Date input #}
  101. {%- macro input_date(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  102. {%- do field.attrs.update(attrs) -%}
  103. {%- if horizontal -%}
  104. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  105. {%- else -%}
  106. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  107. {%- endif -%}
  108. <input type="text"{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}{% if field.has_value %} value="{{ field.value }}"{% endif %}>
  109. {%- endmacro -%}
  110. {# Multiple Checkbox input #}
  111. {%- macro input_checkbox_select_multiple(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  112. {%- do field.attrs.update(attrs) -%}
  113. {%- do classes.append('select-multiple') -%}
  114. <div{{ field_classes(classes) }}>{% for choice in field.choices %}
  115. <label class="checkbox">
  116. <input type="checkbox" name="{{ field.html_name }}" id="{{ field.html_id }}_{{ choice[0] }}" value="{{ choice[0] }}"{% if field.value and choice[0] in field.value %} checked="checked"{% endif %}>
  117. {{ choice[1] }}
  118. </label>{% endfor %}
  119. </div>
  120. {%- endmacro -%}
  121. {# File Upload input #}
  122. {%- macro input_file_clearable(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  123. <input type="file" name="{{ field.html_name }}" id="{{ field.html_id }}" >
  124. {%- endmacro -%}
  125. {# Recaptcha input #}
  126. {%- macro input_recaptcha(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  127. {{ field.attrs.html|safe }}
  128. {%- endmacro -%}
  129. {# RadioSelect input #}
  130. {%- macro input_radio_select(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  131. {%- do field.attrs.update(attrs) -%}
  132. {%- do classes.append('radio-group') -%}
  133. <div{{ field_classes(classes) }}>{% for choice in field.choices %}
  134. <label class="radio">
  135. <input type="radio" name="{{ field.html_name }}" id="{{ field.html_id }}{{ choice[0] }}" value="{{ choice[0] }}"{% if field.value == choice[0] %} checked="checked"{% endif %}>
  136. {{ choice[1] }}
  137. </label>{% endfor %}
  138. </div>
  139. {%- endmacro -%}
  140. {# Select input #}
  141. {%- macro input_select(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  142. {%- do field.attrs.update(attrs) -%}
  143. {%- if horizontal %}
  144. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  145. {%- else -%}
  146. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  147. {%- endif -%}
  148. <select{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}>{% for choice in field.choices %}
  149. <option value="{{ choice[0] }}"{% if field.value == choice[0] %} selected="selected"{% endif %}>{{ choice[1] }}</option>{% endfor %}
  150. </select>
  151. {%- endmacro -%}
  152. {# Text/password input #}
  153. {%- macro input_text(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  154. {%- do field.attrs.update(attrs) -%}
  155. {%- if horizontal -%}
  156. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  157. {%- else -%}
  158. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  159. {%- endif -%}
  160. <input{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}{% if field.attrs.type != 'password' and field.has_value %} value="{{ field.value }}"{% endif %}>
  161. {%- endmacro -%}
  162. {# Textarea input #}
  163. {%- macro input_textarea(field, attrs={'rows': 4}, classes=[], horizontal=false, width=12, nested=false) -%}
  164. {%- do field.attrs.update(attrs) -%}
  165. {%- if horizontal -%}
  166. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  167. {%- else -%}
  168. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  169. {%- endif -%}
  170. <textarea{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}>{% if field.has_value %}{{ field.value }}{% endif %}</textarea>
  171. {%- endmacro -%}
  172. {# YesNoSwitch input #}
  173. {%- macro input_yes_no_switch(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  174. {%- do field.attrs.update(attrs) -%}
  175. {%- do classes.append('yes-no-switch') -%}
  176. <div{{ field_classes(classes) }} id="{{ field.html_id }}_div">
  177. <input name="{{ field.html_name }}" id="{{ field.html_id }}" type="checkbox" value="1"{% if field.value %} checked="checked"{% endif %}>
  178. </div>
  179. {%- endmacro -%}