_forms.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 == "recaptcha" -%}
  51. {{ input_recaptcha(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  52. {%- endif -%}
  53. {%- if field.widget == "radio_select" -%}
  54. {{ input_radio_select(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  55. {%- endif -%}
  56. {%- if field.widget == "select" -%}
  57. {{ input_select(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  58. {%- endif -%}
  59. {%- if field.widget == "checkbox_select_multiple" -%}
  60. {{ input_checkbox_select_multiple(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  61. {%- endif -%}
  62. {%- if field.widget == "text" -%}
  63. {{ input_text(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  64. {%- endif -%}
  65. {%- if field.widget == "textarea" -%}
  66. {{ input_textarea(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  67. {%- endif -%}
  68. {%- if field.widget == "yes_no_switch" -%}
  69. {{ input_yes_no_switch(field, attrs=attrs, classes=[], horizontal=horizontal, width=width, nested=nested) }}
  70. {%- endif %}
  71. {%- endmacro -%}
  72. {# Render form field attributes macro #}
  73. {%- macro field_attrs(attrs={}, extras=[]) -%}
  74. {% for attribute in attrs %} {{ attribute }}="{{ attrs[attribute] }}"{% endfor %}{% for extra in extras %} {{ extra }}{% endfor %}
  75. {%- endmacro -%}
  76. {# Render form field class attribute macro #}
  77. {%- macro field_classes(classes=[]) -%}
  78. {% if classes %} class="{% for class in classes %}{% if not loop.first %} {% endif %}{{ class }}{% endfor %}"{% endif %}
  79. {%- endmacro -%}
  80. {# Checkbox input #}
  81. {%- macro input_checkbox(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  82. <label class="checkbox">
  83. <input type="checkbox" name="{{ field.html_name }}" id="{{ field.html_id }}" value="1"{% if field.value %} checked="checked"{% endif %}>
  84. {% if field.inline is defined %}{{ field.inline }}{% else %}{{ field.label }}{% endif %}
  85. </label>
  86. {%- endmacro -%}
  87. {# Date input #}
  88. {%- macro input_date(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  89. {%- do field.attrs.update(attrs) -%}
  90. {%- if horizontal -%}
  91. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  92. {%- else -%}
  93. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  94. {%- endif -%}
  95. <input type="text"{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}{% if field.has_value %} value="{{ field.value }}"{% endif %}>
  96. {%- endmacro -%}
  97. {# Multiple Checkbox input #}
  98. {%- macro input_checkbox_select_multiple(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  99. {%- do field.attrs.update(attrs) -%}
  100. {%- do classes.append('select-multiple') -%}
  101. <div{{ field_classes(classes) }}>{% for choice in field.choices %}
  102. <label class="checkbox">
  103. <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 %}>
  104. {{ choice[1] }}
  105. </label>{% endfor %}
  106. </div>
  107. {%- endmacro -%}
  108. {# File Upload input #}
  109. {%- macro input_file_clearable(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  110. <input type="file" name="{{ field.html_name }}" id="{{ field.html_id }}" >
  111. {%- endmacro -%}
  112. {# Recaptcha input #}
  113. {%- macro input_recaptcha(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  114. {{ field.attrs.html|safe }}
  115. {%- endmacro -%}
  116. {# RadioSelect input #}
  117. {%- macro input_radio_select(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  118. {%- do field.attrs.update(attrs) -%}
  119. {%- do classes.append('radio-group') -%}
  120. <div{{ field_classes(classes) }}>{% for choice in field.choices %}
  121. <label class="radio">
  122. <input type="radio" name="{{ field.html_name }}" id="{{ field.html_id }}{{ choice[0] }}" value="{{ choice[0] }}"{% if field.value == choice[0] %} checked="checked"{% endif %}>
  123. {{ choice[1] }}
  124. </label>{% endfor %}
  125. </div>
  126. {%- endmacro -%}
  127. {# Select input #}
  128. {%- macro input_select(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  129. {%- do field.attrs.update(attrs) -%}
  130. {%- if horizontal %}
  131. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  132. {%- else -%}
  133. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  134. {%- endif -%}
  135. <select{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}>{% for choice in field.choices %}
  136. <option value="{{ choice[0] }}"{% if field.value == choice[0] %} selected="selected"{% endif %}>{{ choice[1] }}</option>{% endfor %}
  137. </select>
  138. {%- endmacro -%}
  139. {# Text/password input #}
  140. {%- macro input_text(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. <input{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}{% if field.attrs.type != 'password' and field.has_value %} value="{{ field.value }}"{% endif %}>
  148. {%- endmacro -%}
  149. {# Textarea input #}
  150. {%- macro input_textarea(field, attrs={'rows': 4}, classes=[], horizontal=false, width=12, nested=false) -%}
  151. {%- do field.attrs.update(attrs) -%}
  152. {%- if horizontal -%}
  153. {%- do classes.append('span' ~ (widthratio(field.width, 100, width) - 2)) -%}
  154. {%- else -%}
  155. {%- do classes.append('span' ~ widthratio(field.width, 100, width)) -%}
  156. {%- endif -%}
  157. <textarea{{ field_attrs(field.attrs) }}{{ field_classes(classes) }}>{% if field.has_value %}{{ field.value }}{% endif %}</textarea>
  158. {%- endmacro -%}
  159. {# YesNoSwitch input #}
  160. {%- macro input_yes_no_switch(field, attrs={}, classes=[], horizontal=false, width=12, nested=false) -%}
  161. {%- do field.attrs.update(attrs) -%}
  162. {%- do classes.append('yes-no-switch') -%}
  163. <div{{ field_classes(classes) }} id="{{ field.html_id }}_div">
  164. <input name="{{ field.html_name }}" id="{{ field.html_id }}" type="checkbox" value="1"{% if field.value %} checked="checked"{% endif %}>
  165. </div>
  166. {%- endmacro -%}