wtf.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. {% macro form_errors(form, hiddens=True) %}
  2. {%- if form.errors %}
  3. {%- for fieldname, errors in form.errors.items() %}
  4. {%- if bootstrap_is_hidden_field(form[fieldname]) and hiddens or
  5. not bootstrap_is_hidden_field(form[fieldname]) and hiddens != 'only' %}
  6. {%- for error in errors %}
  7. <p class="error">{{error}}</p>
  8. {%- endfor %}
  9. {%- endif %}
  10. {%- endfor %}
  11. {%- endif %}
  12. {%- endmacro %}
  13. {% macro _hz_form_wrap(horizontal_columns, form_type, add_group=False, required=False) %}
  14. {% if form_type == "horizontal" %}
  15. {% if add_group %}<div class="form-group{% if required %} required{% endif %}">{% endif %}
  16. <div class="col-{{horizontal_columns[0]}}-offset-{{horizontal_columns[1]}}
  17. col-{{horizontal_columns[0]}}-{{horizontal_columns[2]}}
  18. ">
  19. {% endif %}
  20. {{caller()}}
  21. {% if form_type == "horizontal" %}
  22. {% if add_group %}</div>{% endif %}
  23. </div>
  24. {% endif %}
  25. {% endmacro %}
  26. {% macro form_field(field,
  27. form_type="basic",
  28. horizontal_columns=('lg', 2, 10),
  29. button_map={}) %}
  30. {# this is a workaround hack for the more straightforward-code of just passing required=required parameter. older versions of wtforms do not have
  31. the necessary fix for required=False attributes, but will also not set the required flag in the first place. we skirt the issue using the code below #}
  32. {% if field.flags.required and not required in kwargs %}
  33. {% set kwargs = dict(required=True, **kwargs) %}
  34. {% endif %}
  35. {% if field.widget.input_type == 'checkbox' %}
  36. {% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %}
  37. <div class="checkbox">
  38. <label>
  39. {{field()|safe}} {{field.label.text|safe}}
  40. </label>
  41. </div>
  42. {% endcall %}
  43. {%- elif field.type == 'RadioField' -%}
  44. {# note: A cleaner solution would be rendering depending on the widget,
  45. this is just a hack for now, until I can think of something better #}
  46. {% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %}
  47. {% for item in field -%}
  48. <div class="radio">
  49. <label>
  50. {{item|safe}} {{item.label.text|safe}}
  51. </label>
  52. </div>
  53. {% endfor %}
  54. {% endcall %}
  55. {%- elif field.type == 'SubmitField' -%}
  56. {# deal with jinja scoping issues? #}
  57. {% set field_kwargs = kwargs %}
  58. {# note: same issue as above - should check widget, not field type #}
  59. {% call _hz_form_wrap(horizontal_columns, form_type, True, required=required) %}
  60. {{field(class='btn btn-%s' % button_map.get(field.name, 'default'),
  61. **field_kwargs)}}
  62. {% endcall %}
  63. {%- elif field.type == 'FormField' -%}
  64. {# note: FormFields are tricky to get right and complex setups requiring
  65. these are probably beyond the scope of what this macro tries to do.
  66. the code below ensures that things don't break horribly if we run into
  67. one, but does not try too hard to get things pretty. #}
  68. <fieldset>
  69. <legend>{{field.label}}</legend>
  70. {%- for subfield in field %}
  71. {% if not bootstrap_is_hidden_field(subfield) -%}
  72. {{ form_field(subfield,
  73. form_type=form_type,
  74. horizontal_columns=horizontal_columns,
  75. button_map=button_map) }}
  76. {%- endif %}
  77. {%- endfor %}
  78. </fieldset>
  79. {% else -%}
  80. <div class="form-group {% if field.errors %} has-error{% endif -%}
  81. {%- if field.flags.required %} required{% endif -%}
  82. ">
  83. {%- if form_type == "inline" %}
  84. {{field.label(class="sr-only")|safe}}
  85. {% if field.type == 'FileField' %}
  86. {{field(**kwargs)|safe}}
  87. {% else %}
  88. {{field(class="form-control", **kwargs)|safe}}
  89. {% endif %}
  90. {% elif form_type == "horizontal" %}
  91. {{field.label(class="control-label " + (
  92. " col-%s-%s" % horizontal_columns[0:2]
  93. ))|safe}}
  94. <div class=" col-{{horizontal_columns[0]}}-{{horizontal_columns[2]}}">
  95. {% if field.type == 'FileField' %}
  96. {{field(**kwargs)|safe}}
  97. {% else %}
  98. {{field(class="form-control", **kwargs)|safe}}
  99. {% endif %}
  100. </div>
  101. {%- if field.errors %}
  102. {%- for error in field.errors %}
  103. {% call _hz_form_wrap(horizontal_columns, form_type, required=required) %}
  104. <p class="help-block">{{error}}</p>
  105. {% endcall %}
  106. {%- endfor %}
  107. {%- elif field.description -%}
  108. {% call _hz_form_wrap(horizontal_columns, form_type, required=required) %}
  109. <p class="help-block">{{field.description|safe}}</p>
  110. {% endcall %}
  111. {%- endif %}
  112. {%- else -%}
  113. {{field.label(class="control-label")|safe}}
  114. {% if field.type == 'FileField' %}
  115. {{field(**kwargs)|safe}}
  116. {% else %}
  117. {{field(class="form-control", **kwargs)|safe}}
  118. {% endif %}
  119. {%- if field.errors %}
  120. {%- for error in field.errors %}
  121. <p class="help-block">{{error}}</p>
  122. {%- endfor %}
  123. {%- elif field.description -%}
  124. <p class="help-block">{{field.description|safe}}</p>
  125. {%- endif %}
  126. {%- endif %}
  127. </div>
  128. {% endif %}
  129. {% endmacro %}
  130. {# valid form types are "basic", "inline" and "horizontal" #}
  131. {% macro quick_form(form,
  132. action="",
  133. method="post",
  134. extra_classes=None,
  135. role="form",
  136. form_type="basic",
  137. horizontal_columns=('lg', 2, 10),
  138. enctype=None,
  139. button_map={},
  140. id="",
  141. novalidate=False) %}
  142. {#-
  143. action="" is what we want, from http://www.ietf.org/rfc/rfc2396.txt:
  144. 4.2. Same-document References
  145. A URI reference that does not contain a URI is a reference to the
  146. current document. In other words, an empty URI reference within a
  147. document is interpreted as a reference to the start of that document,
  148. and a reference containing only a fragment identifier is a reference
  149. to the identified fragment of that document. Traversal of such a
  150. reference should not result in an additional retrieval action.
  151. However, if the URI reference occurs in a context that is always
  152. intended to result in a new request, as in the case of HTML's FORM
  153. element, then an empty URI reference represents the base URI of the
  154. current document and should be replaced by that URI when transformed
  155. into a request.
  156. -#}
  157. {#- if any file fields are inside the form and enctype is automatic, adjust
  158. if file fields are found. could really use the equalto test of jinja2
  159. here, but latter is not available until 2.8
  160. warning: the code below is guaranteed to make you cry =(
  161. #}
  162. {%- set _enctype = [] %}
  163. {%- if enctype is none -%}
  164. {%- for field in form %}
  165. {%- if field.type == 'FileField' %}
  166. {#- for loops come with a fairly watertight scope, so this list-hack is
  167. used to be able to set values outside of it #}
  168. {%- set _ = _enctype.append('multipart/form-data') -%}
  169. {%- endif %}
  170. {%- endfor %}
  171. {%- else %}
  172. {% set _ = _enctype.append(enctype) %}
  173. {%- endif %}
  174. <form
  175. {%- if action != None %} action="{{action}}"{% endif -%}
  176. {%- if id %} id="{{id}}"{% endif -%}
  177. {%- if method %} method="{{method}}"{% endif %}
  178. class="form
  179. {%- if extra_classes %} {{extra_classes}}{% endif -%}
  180. {%- if form_type == "horizontal" %} form-horizontal
  181. {%- elif form_type == "inline" %} form-inline
  182. {%- endif -%}
  183. "
  184. {%- if _enctype[0] %} enctype="{{_enctype[0]}}"{% endif -%}
  185. {%- if role %} role="{{role}}"{% endif -%}
  186. {%- if novalidate %} novalidate{% endif -%}
  187. >
  188. {{ form.hidden_tag() }}
  189. {{ form_errors(form, hiddens='only') }}
  190. {%- for field in form %}
  191. {% if not bootstrap_is_hidden_field(field) -%}
  192. {{ form_field(field,
  193. form_type=form_type,
  194. horizontal_columns=horizontal_columns,
  195. button_map=button_map) }}
  196. {%- endif %}
  197. {%- endfor %}
  198. </form>
  199. {%- endmacro %}