field.html.twig 6.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. {% set originalValue = originalValue is defined ? originalValue : value %}
  2. {% set toggleableChecked = field.toggleable and (originalValue is not null and originalValue is not empty) %}
  3. {% set isDisabledToggleable = field.toggleable and not toggleableChecked %}
  4. {% set value = (is_object(value) or value is null ? field.default : value) %}
  5. {% set cookie_name = 'forms-' ~ form.name ~ '-' ~ field.name %}
  6. {% set value = (is_object(value) or value is null ? (get_cookie(cookie_name) is null ? field.default : get_cookie(cookie_name)) : value) %}
  7. {% set errors = attribute(form.messages, field.name) %}
  8. {% block field %}
  9. <div class="{{ form_field_outer_classes ?: 'form-field' }} {{ field.outerclasses }} {% if errors %} has-errors{% endif %} {% block outer_field_classes %}{% endblock %}">
  10. {% block contents %}
  11. {% if field.label is not same as(false) %}
  12. <div class="{{ form_field_outer_label_classes ?: 'form-label' }} {{ field.labelclasses }}">
  13. <label class="{{ form_field_label_classes ?: 'inline' }}" {% if field.id is defined %}for="{{ field.id|e }}" {% endif %} >
  14. {% block label %}
  15. {% if field.help %}
  16. {% if field.markdown %}
  17. <span class="tooltip" data-asTooltip-position="w" title="{{ field.help|t|markdown(false)|e }}">{{ field.label|markdown(false)|default(field.name|capitalize)|t }}</span>
  18. {% else %}
  19. <span class="tooltip" data-asTooltip-position="w" title="{{ field.help|t|e }}">{{ field.label|default(field.name|capitalize)|t }}</span>
  20. {% endif %}
  21. {% else %}
  22. {% if field.markdown %}
  23. {{ field.label|markdown(false)|default(field.name|capitalize)|t }}
  24. {% else %}
  25. {{ field.label|default(field.name|capitalize)|t }}
  26. {% endif %}
  27. {% endif %}
  28. {{ field.validate.required in ['on', 'true', 1] ? '<span class="required">*</span>' }}
  29. {% endblock %}
  30. </label>
  31. </div>
  32. {% endif %}
  33. <div class="{{ form_field_outer_data_classes ?: 'form-data' }} {{ field.dataclasses }}"
  34. {% block global_attributes %}
  35. data-grav-field="{{ field.type }}"
  36. data-grav-disabled="{{ originalValue is null ? 'true' : 'false' }}"
  37. data-grav-default="{{ field.default|json_encode()|e('html_attr') }}"
  38. {% endblock %}
  39. >
  40. {% block group %}
  41. {% block input %}
  42. <div class="{{ form_field_wrapper_classes ?: 'form-input-wrapper' }} {{ field.size }} {{ field.wrapper_classes }}">
  43. {% block prepend %}{% endblock prepend %}
  44. <input
  45. {# required attribute structures #}
  46. name="{{ (scope ~ field.name)|fieldName }}"
  47. value="{{ value|join(', ')|e('html_attr') }}"
  48. {# input attribute structures #}
  49. {% block input_attributes %}
  50. class="{{ form_field_input_classes }} {{ field.classes }}"
  51. {% if field.id is defined %}id="{{ field.id|e }}" {% endif %}
  52. {% if field.style is defined %}style="{{ field.style|e }}" {% endif %}
  53. {% if field.disabled or isDisabledToggleable %}disabled="disabled"{% endif %}
  54. {% if field.placeholder %}placeholder="{{ field.placeholder|t }}"{% endif %}
  55. {% if field.autofocus in ['on', 'true', 1] %}autofocus="autofocus"{% endif %}
  56. {% if field.novalidate in ['on', 'true', 1] %}novalidate="novalidate"{% endif %}
  57. {% if field.readonly in ['on', 'true', 1] %}readonly="readonly"{% endif %}
  58. {% if field.autocomplete in ['on', 'off'] %}autocomplete="{{ field.autocomplete }}"{% endif %}
  59. {% if field.attributes is defined %}
  60. {% for attribute in field.attributes %}
  61. {{ attribute.name }}="{{ attribute.value|e }}"
  62. {% endfor %}
  63. {% endif %}
  64. {% if field.validate.required in ['on', 'true', 1] %}required="required"{% endif %}
  65. {% if field.validate.pattern %}pattern="{{ field.validate.pattern|e }}"{% endif %}
  66. {% if field.validate.message %}title="{{ field.validate.message|t|e }}"
  67. {% elseif field.title is defined %}title="{{ field.title|t|e }}" {% endif %}
  68. {% endblock %}
  69. />
  70. {% block append %}{% endblock append %}
  71. {% if form.inline_errors and errors %}
  72. <div class="{{ form_errors_classes ?: 'form-errors' }}">
  73. {% for error in errors %}
  74. <p class="form-message"><i class="fa fa-exclamation-circle"></i> {{ error }}</p>
  75. {% endfor %}
  76. </div>
  77. {% endif %}
  78. </div>
  79. {% endblock %}
  80. {% endblock %}
  81. {% if field.description %}
  82. <div class="form-extra-wrapper {{ field.size|e }} {{ field.wrapper_classes }}">
  83. <span class="form-description">
  84. {% if field.markdown %}
  85. {{ field.description|tu|markdown(false)|raw }}
  86. {% else %}
  87. {{ field.description|tu|raw }}
  88. {% endif %}
  89. </span>
  90. </div>
  91. {% endif %}
  92. </div>
  93. {% endblock %}
  94. </div>
  95. {% endblock %}