bootstrap_wtf.html 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. {% macro form_field(field, autofocus=False) %}
  2. {%- if field.type == 'BooleanField' %}
  3. <div class="form-check mb-3">
  4. {{ field(class='form-check-input') }}
  5. {{ field.label(class='form-check-label') }}
  6. </div>
  7. {%- elif field.type == 'RadioField' %}
  8. {{ field.label(class='form-label') }}
  9. {%- for item in field %}
  10. <div class="form-check{% if loop.last %} mb-3{% endif %}">
  11. {{ item(class='form-check-input') }}
  12. {{ item.label(class='form-check-label') }}
  13. </div>
  14. {%- endfor %}
  15. {%- elif field.type == 'SelectField' %}
  16. {{ field.label(class='form-label') }}
  17. {{ field(class='form-select mb-3') }}
  18. {%- elif field.type == 'TextAreaField' %}
  19. <div class="mb-3">
  20. {{ field.label(class='form-label') }}
  21. {% if autofocus %}
  22. {{ field(class='form-control' + (' is-invalid' if field.errors else ''), autofocus=True) }}
  23. {% else %}
  24. {{ field(class='form-control' + (' is-invalid' if field.errors else '')) }}
  25. {% endif %}
  26. {# {%- for error in field.errors %}
  27. <div class="invalid-feedback">{{ error }}</div>
  28. {%- endfor %} #}
  29. </div>
  30. {%- elif field.type == 'SubmitField' %}
  31. {{ field(class='btn btn-primary mb-3') }}
  32. {%- else %}
  33. <div class="mb-3">
  34. {{ field.label(class='form-label') }}
  35. {% if autofocus %}
  36. {{ field(class='form-control' + (' is-invalid' if field.errors else ''), autofocus=True) }}
  37. {% else %}
  38. {{ field(class='form-control' + (' is-invalid' if field.errors else '')) }}
  39. {% endif %}
  40. {# {%- for error in field.errors %}
  41. <div class="invalid-feedback">{{ error }}</div>
  42. {%- endfor %} #}
  43. </div>
  44. {%- endif %}
  45. {% endmacro %}
  46. {% macro quick_form(form, action="", method="post", id="", novalidate=False) %}
  47. <form
  48. {%- if action != None %} action="{{ action }}"{% endif -%}
  49. {%- if method %} method="{{ method }}"{% endif %}
  50. {%- if id %} id="{{ id }}"{% endif -%}
  51. {%- if novalidate %} novalidate{% endif -%}>
  52. {{ form.hidden_tag() }}
  53. {%- for field, errors in form.errors.items() %}
  54. {%- if form[field].widget.input_type == 'hidden' %}
  55. {%- for error in errors %}
  56. <div class="invalid-feedback">{{ error }}</div>
  57. {%- endfor %}
  58. {%- endif %}
  59. {%- endfor %}
  60. {% set ns = namespace(first_field=true) %}
  61. {%- for field in form %}
  62. {% if field.widget.input_type != 'hidden' -%}
  63. {{ form_field(field, ns.first_field) }}
  64. {% set ns.first_field = false %}
  65. {%- endif %}
  66. {%- endfor %}
  67. </form>
  68. {% endmacro %}