_forms.html 9.7 KB

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