_forms.html 8.4 KB

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