field.html.twig 7.4 KB

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