field.html.twig 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 in ['on', 'off', 'new-password'] %}autocomplete="{{ field.autocomplete }}"{% endif %}
  64. {% if field.attributes is defined %}
  65. {% for attribute in field.attributes %}
  66. {{ attribute.name }}="{{ attribute.value|e }}"
  67. {% endfor %}
  68. {% endif %}
  69. {% if required %}required="required"{% endif %}
  70. {% if field.validate.pattern %}pattern="{{ field.validate.pattern|e }}"{% endif %}
  71. {% if field.validate.message %}title="{{ field.validate.message|t|e }}"
  72. {% elseif field.title is defined %}title="{{ field.title|t|e }}" {% endif %}
  73. {% if field.datasets %}
  74. {% for datakey, datavalue in field.datasets %}
  75. data-{{ datakey }}="{{ datavalue|e('html_attr') }}"
  76. {% endfor %}
  77. {% endif %}
  78. {% endblock %}
  79. />
  80. {% block append %}{% endblock append %}
  81. {% if inline_errors and errors %}
  82. <div class="{{ form_errors_classes ?: 'form-errors' }}">
  83. {% for error in errors %}
  84. <p class="form-message"><i class="fa fa-exclamation-circle"></i> {{ error }}</p>
  85. {% endfor %}
  86. </div>
  87. {% endif %}
  88. </div>
  89. {% endblock %}
  90. {% endblock %}
  91. {% if field.description %}
  92. <div class="form-extra-wrapper {{ field.size|e }} {{ field.wrapper_classes }}">
  93. <span class="form-description">
  94. {% if field.markdown %}
  95. {{ field.description|t|markdown(false)|raw }}
  96. {% else %}
  97. {{ field.description|t|raw }}
  98. {% endif %}
  99. </span>
  100. </div>
  101. {% endif %}
  102. </div>
  103. {% endblock %}
  104. </div>
  105. {% endblock %}