forms.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. {# Forms macros for rendering forms and fields and stuff in templates. #}
  2. {% macro hidden_fields(form) -%}
  3. <input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}">
  4. {% for field in form.hidden_fields() %}
  5. <input type="hidden" name="{{ field.html_name }}" value="{{ field.value() }}">
  6. {% endfor %}
  7. {%- endmacro %}
  8. {% macro row(_field, label=None, help_text=None, attrs=None) -%}
  9. <div id="{{ _field.html_name }}-control-group" class="control-group{% if _field.errors %} error{% endif %}">
  10. <label class="control-label" for="id_{{ _field.html_name }}">{% if label %}{{ label }}{% elif _field.label %}{{ _field.label }}{% else %}{{ _field.html_name }}{% endif %}:</label>
  11. <div class="controls">
  12. {% if attrs == None %}{% set attrs = {} %}{% endif %}
  13. {% if _field.field.widget.__class__.__name__ == 'ForumTOS' %}{% do attrs.update({'inline': make_tos_label()}) %}{% endif %}
  14. {% if _field.field.widget.__class__.__name__ in ('CheckboxInput', 'ForumTOS') %}
  15. <label class="checkbox">
  16. {{ field(_field, attrs=attrs)|trim }}
  17. {% if 'inline' in attrs %}
  18. {{ attrs.inline }}
  19. {% else %}
  20. {% if help_text %}
  21. {{ help_text }}
  22. {% elif _field.help_text %}
  23. {{ _field.help_text }}
  24. {% endif %}
  25. {% endif %}
  26. </label>
  27. {% else %}
  28. {{ field(_field, attrs=attrs)|trim }}
  29. {% endif %}
  30. {% for error in _field.errors %}
  31. <p class="help-block" style="font-weight: bold;">{{ error }}</p>
  32. {% endfor %}
  33. {% if 'inline' in attrs or _field.field.widget.__class__.__name__ not in ('CheckboxInput', 'ForumTOS') %}
  34. {% if help_text %}
  35. <p class="help-block">{{ help_text }}</p>
  36. {% elif _field.help_text %}
  37. <p class="help-block">{{ _field.help_text }}</p>
  38. {% endif %}
  39. {% endif %}
  40. </div>
  41. </div>
  42. {%- endmacro %}
  43. {% macro repeat(form, fields, label=None, help_text=None, attrs=None) -%}
  44. <div id="{{ fields[0].html_name }}-control-group" class="control-group{% if fields[0].errors or fields[1].errors or (fields[0].name ~ '_' ~ fields[1].name) in form.errors %} error{% endif %}">
  45. <label class="control-label" for="id_{{ fields[0].html_name }}">{% if label %}{{ label }}{% elif fields[0].label %}{{ fields[0].label }}{% else %}{{ fields[0].html_name }}{% endif %}</label>
  46. <div class="controls controls-nested">
  47. {% if attrs == None %}{% set attrs = ({}, {}) %}{% endif %}
  48. <div class="row">
  49. <div{% if 'class' in attrs[0] %} class="{{ attrs[0].class }}"{% endif %}>{{ field(fields[0], attrs=attrs[0]) }}</div>
  50. <div{% if 'class' in attrs[1] %} class="{{ attrs[1].class }}"{% endif %}>{{ field(fields[1], attrs=attrs[1]) }}</div>
  51. </div>
  52. {% for error in form.errors[(fields[0].name ~ '_' ~ fields[1].name)] %}
  53. <p class="help-block" style="font-weight: bold;">{{ error }}</p>
  54. {% endfor %}
  55. {% for error in fields[0].errors %}
  56. <p class="help-block" style="font-weight: bold;">{{ error }}</p>
  57. {% endfor %}
  58. {% for error in fields[1].errors %}
  59. {% if not error in fields[0].errors %}
  60. <p class="help-block" style="font-weight: bold;">{{ error }}</p>
  61. {% endif %}
  62. {% endfor %}
  63. {% if help_text %}
  64. <p class="help-block">{{ help_text }}</p>
  65. {% elif fields[0].help_text %}
  66. <p class="help-block">{{ fields[0].help_text }}</p>
  67. {% endif %}
  68. </div>
  69. </div>
  70. {%- endmacro %}
  71. {% macro make_tos_label() -%}
  72. {% trans forum_tos=make_tos_link()|safe %}I have read and accept this forums {{forum_tos}}.{% endtrans %}
  73. {%- endmacro %}
  74. {% macro make_tos_link() -%}
  75. <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>
  76. {%- endmacro %}
  77. {% macro captcha(form, attrs=None) -%}
  78. {% if 'recaptcha' in form.fields %}
  79. {{ row(form.recaptcha) }}
  80. {% endif %}
  81. {% if 'captcha_qa' in form.fields %}
  82. {{ row(form.captcha_qa, attrs=attrs) }}
  83. {% endif %}
  84. {%- endmacro %}
  85. {% macro field(_field, attrs=None) -%}
  86. {% set widget = _field.field.widget.__class__.__name__ %}
  87. {% set context = _field.field.widget.get_context(_field.html_name, _field.value(), attrs=attrs) %}
  88. {% if 'inline' in context.attrs %}{% do context.attrs.pop('inline') %}{% endif %}
  89. {% if widget == 'Textarea' %}
  90. {{ _textarea(_field, context) }}
  91. {% elif widget == 'YesNoSwitch' %}
  92. {{ _yesno(_field, context) }}
  93. {% elif widget == 'Select' %}
  94. {{ _select(_field, context) }}
  95. {% elif widget == 'RadioSelect' %}
  96. {{ _radio_select(_field, context) }}
  97. {% elif widget == 'SelectMultiple' %}
  98. {{ _select_multiple(_field, context) }}
  99. {% elif widget == 'CheckboxSelectMultiple' %}
  100. {{ _checkbox_select(_field, context) }}
  101. {% elif widget == 'ReCaptchaWidget' %}
  102. {{ _field.field.widget.render()|safe }}
  103. {% else %}
  104. {{ _input(_field, context) }}
  105. {% endif %}
  106. {%- endmacro %}
  107. {% macro attributes(attrs) -%}
  108. {% for name, value in attrs.items() %} {{ name }}{% if value != True %}="{{ value }}"{% endif %}{% endfor %}
  109. {%- endmacro %}
  110. {% macro _input(_field, context) -%}
  111. <input type="{{ context.type }}" id="id_{{ context.name }}" name="{{ context.name }}" {{ attributes(context.attrs)|trim }}{% if context.type != 'password' and 'value' in context and context.value is not none %} value="{{ context.value }}"{% endif %}>
  112. {%- endmacro %}
  113. {% macro _textarea(_field, context) -%}
  114. <textarea id="id_{{ context.name }}" name="{{ context.name }}" {{ attributes(context.attrs)|trim }}>{% if 'value' in context and context.value|length > 0 %}{{ context.value }}{% endif %}</textarea>
  115. {%- endmacro %}
  116. {% macro _yesno(_field, context) -%}
  117. <div id="id_{{ context.name }}_div" class="yes-no-switch{% if 'class' in context.attrs %} {{ context.attrs.class }}{% endif %}">
  118. {{ _input(_field, context) }}
  119. </div>
  120. {%- endmacro %}
  121. {% macro _select(_field, context) -%}
  122. <select id="id_{{ context.name }}" name="{{ context.name }}" {{ attributes(context.attrs)|trim }}>
  123. {% if context['optgroups']|length > 1 %}
  124. {% for optgroup in context['optgroups'] %}
  125. <optgroup label="{{ optgroup[0] }}">
  126. {% for option in optgroup[1] %}
  127. <option value="{{ option[0] }}"{% if 'value' in context and option[0] in context.value %} selected="selected"{% endif %}>{{ option[1] }}</option>
  128. {% endfor %}
  129. </optgroup>
  130. {% endfor %}
  131. {% else %}
  132. {% for option in context['optgroups'][0][1] %}
  133. <option value="{{ option[0] }}"{% if 'value' in context and option[0] in context.value %} selected="selected"{% endif %}>{{ option[1] }}</option>
  134. {% endfor %}
  135. {% endif %}
  136. </select>
  137. {%- endmacro %}
  138. {% macro _radio_select(_field, context) -%}
  139. <div class="radio-group">
  140. {% for option in context['optgroups'][0][1] %}
  141. <label class="radio">
  142. <input type="radio" name="{{ context.name }}" id="id_{{ context.name }}_{{ option[0] }}" value="{{ option[0] }}"{% if 'value' in context and option[0] in context.value %} checked="checked"{% endif %}>
  143. {{ option[1] }}
  144. </label>
  145. {% endfor %}
  146. </div>
  147. {%- endmacro %}
  148. {% macro _select_multiple(_field, context) -%}
  149. <select id="id_{{ context.name }}" name="{{ context.name }}" {{ attributes(context.attrs)|trim }}>
  150. {% if context['optgroups']|length > 1 %}
  151. {% for optgroup in context['optgroups'] %}
  152. <optgroup label="{{ optgroup[0] }}">
  153. {% for option in optgroup[1] %}
  154. <option value="{{ option[0] }}"{% if 'value' in context and option[0] in context.value %} selected="selected"{% endif %}>{{ option[1] }}</option>
  155. {% endfor %}
  156. </optgroup>
  157. {% endfor %}
  158. {% else %}
  159. {% for option in context['optgroups'][0][1] %}
  160. <option value="{{ option[0] }}"{% if 'value' in context and option[0] in context.value %} selected="selected"{% endif %}>{{ option[1] }}</option>
  161. {% endfor %}
  162. {% endif %}
  163. </select>
  164. {%- endmacro %}
  165. {% macro _checkbox_select(_field, context) %}
  166. <div class="select-multiple">
  167. {% for option in context['optgroups'][0][1] %}
  168. <label class="checkbox">
  169. <input type="checkbox" name="{{ context.name }}" id="id_{{ context.name }}_{{ option[0] }}" value="{{ option[0] }}"{% if 'value' in context and option[0] in context.value %} checked="checked"{% endif %}>
  170. {{ option[1] }}
  171. </label>
  172. {% endfor %}
  173. </div>
  174. {% endmacro %}