form_admin_fields.html.twig 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. {#
  2. This file is part of the Sonata package.
  3. (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  4. For the full copyright and license information, please view the LICENSE
  5. file that was distributed with this source code.
  6. #}
  7. {% extends 'form_div_layout.html.twig' %}
  8. {% block form_widget -%}
  9. {{ parent() }}
  10. {% if sonata_help is defined and sonata_help %}
  11. <span class="help-block sonata-ba-field-widget-help">{{ sonata_help|raw }}</span>
  12. {% endif %}
  13. {%- endblock form_widget %}
  14. {% block form_widget_simple %}
  15. {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  16. {{ parent() }}
  17. {% endblock form_widget_simple %}
  18. {% block textarea_widget %}
  19. {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  20. {{ parent() }}
  21. {% endblock textarea_widget %}
  22. {% block money_widget -%}
  23. {% if money_pattern == '{{ widget }}' %}
  24. {{- block('form_widget_simple') -}}
  25. {% else %}
  26. {% set currencySymbol = money_pattern|replace({'{{ widget }}': ''})|trim %}
  27. {% if money_pattern matches '/^{{ widget }}/' %}
  28. <div class="input-group">
  29. {{- block('form_widget_simple') -}}
  30. <span class="input-group-addon">{{ currencySymbol }}</span>
  31. </div>
  32. {% elseif money_pattern matches '/{{ widget }}$/' %}
  33. <div class="input-group">
  34. <span class="input-group-addon">{{ currencySymbol }}</span>
  35. {{- block('form_widget_simple') -}}
  36. </div>
  37. {% endif %}
  38. {% endif %}
  39. {%- endblock money_widget %}
  40. {% block percent_widget %}
  41. {% spaceless %}
  42. {% set type = type|default('text') %}
  43. <div class="input-group">
  44. {{ block('form_widget_simple') }}
  45. <span class="input-group-addon">%</span>
  46. </div>
  47. {% endspaceless %}
  48. {% endblock percent_widget %}
  49. {# Labels #}
  50. {% block form_label %}
  51. {% spaceless %}
  52. {% set label_class = "" %}
  53. {% if sonata_admin.admin and sonata_admin.admin.getConfigurationPool().getOption('form_type') == 'horizontal' %}
  54. {% set label_class = " control-label col-sm-3" %}
  55. {% else %}
  56. {% set label_class = " control-label" %}
  57. {% endif%}
  58. {#{{ sonata_admin.admin.getConfigurationPool().getOption('form_type') }}#}
  59. {% if label is not sameas(false) %}
  60. {% set label_attr = label_attr|merge({'class': label_attr.class|default('') ~ label_class }) %}
  61. {% if not compound %}
  62. {% set label_attr = label_attr|merge({'for': id}) %}
  63. {% endif %}
  64. {% if required %}
  65. {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
  66. {% endif %}
  67. {% if label is empty %}
  68. {% set label = name|humanize %}
  69. {% endif %}
  70. {% if in_list_checkbox is defined and in_list_checkbox and widget is defined %}
  71. <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  72. {{ widget|raw }}
  73. <span>
  74. {% if not sonata_admin.admin %}
  75. {{- label|trans({}, translation_domain) -}}
  76. {% else %}
  77. {{- label|trans({}, sonata_admin.field_description.translationDomain) -}}
  78. {% endif%}
  79. </span>
  80. </label>
  81. {% else %}
  82. <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
  83. {% if not sonata_admin.admin%}
  84. {{- label|trans({}, translation_domain) -}}
  85. {% else %}
  86. {{ sonata_admin.admin.trans(label, {}, sonata_admin.field_description.translationDomain) }}
  87. {% endif %}
  88. </label>
  89. {% endif %}
  90. {% endif %}
  91. {% endspaceless %}
  92. {% endblock form_label %}
  93. {% block choice_widget_expanded %}
  94. {% spaceless %}
  95. {% set attr = attr|merge({'class': attr.class|default('') ~ ' list-unstyled'}) %}
  96. <ul {{ block('widget_container_attributes') }}>
  97. {% for child in form %}
  98. <li>
  99. {% set form_widget_content %}
  100. {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  101. {% endset %}
  102. {{ form_label(child, child.vars.label|default(null), { 'in_list_checkbox' : true, 'widget' : form_widget_content } ) }}
  103. </li>
  104. {% endfor %}
  105. </ul>
  106. {% endspaceless %}
  107. {% endblock choice_widget_expanded %}
  108. {% block choice_widget_collapsed %}
  109. {% spaceless %}
  110. {% if required and empty_value is none and not empty_value_in_choices and not multiple %}
  111. {% set required = false %}
  112. {% endif %}
  113. {% if sonata_admin.admin and not sonata_admin.admin.getConfigurationPool().getOption('use_select2') %}
  114. {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  115. {% endif %}
  116. {% if sortable and multiple %}
  117. {{ block('sonata_type_choice_multiple_sortable') }}
  118. {% else %}
  119. <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %} >
  120. {% if empty_value is not none %}
  121. <option value=""{% if required and value is empty %} selected="selected"{% endif %}>
  122. {% if not sonata_admin.admin %}
  123. {{- empty_value|trans({}, translation_domain) -}}
  124. {% else %}
  125. {{- empty_value|trans({}, sonata_admin.field_description.translationDomain) -}}
  126. {% endif%}
  127. </option>
  128. {% endif %}
  129. {% if preferred_choices|length > 0 %}
  130. {% set options = preferred_choices %}
  131. {{ block('choice_widget_options') }}
  132. {% if choices|length > 0 %}
  133. <option disabled="disabled">{{ separator }}</option>
  134. {% endif %}
  135. {% endif %}
  136. {% set options = choices %}
  137. {{ block('choice_widget_options') }}
  138. </select>
  139. {% endif %}
  140. {% endspaceless %}
  141. {% endblock choice_widget_collapsed %}
  142. {% block form_row %}
  143. {% set label_class = "" %}
  144. {% set div_class = "" %}
  145. {% if sonata_admin.admin and sonata_admin.admin.getConfigurationPool().getOption('form_type') == 'horizontal' %}
  146. {% set label_class = "control-label col-sm-3" %}
  147. {% set div_class = "col-sm-9 col-md-9" %}
  148. {% else %}
  149. {% set label_class = "control-label" %}
  150. {% endif%}
  151. {% if sonata_admin is not defined or not sonata_admin_enabled or not sonata_admin.field_description %}
  152. <div class="form-group {% if errors|length > 0%} has-error{% endif %}">
  153. {{ form_label(form, label|default(null)) }}
  154. <div class="{% if label is sameas(false) %}sonata-collection-row-without-label{% endif %}">
  155. {{ form_widget(form, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  156. {% if errors|length > 0 %}
  157. <div class="help-block sonata-ba-field-error-messages">
  158. {{ form_errors(form) }}
  159. </div>
  160. {% endif %}
  161. </div>
  162. </div>
  163. {% else %}
  164. <div class="form-group{% if errors|length > 0%} has-error{%endif%}" id="sonata-ba-field-container-{{ id }}">
  165. {% block label %}
  166. {% if sonata_admin.field_description.options.name is defined %}
  167. {{ form_label(form, sonata_admin.field_description.options.name, { 'attr' : {'class' : label_class} }) }}
  168. {% else %}
  169. {{ form_label(form, label|default(null), { 'attr' : {'class' : label_class} }) }}
  170. {% endif %}
  171. {% endblock %}
  172. {% set has_label = sonata_admin.field_description.options.name is defined or label is not sameas(false) %}
  173. <div class="{{ div_class }} sonata-ba-field sonata-ba-field-{{ sonata_admin.edit }}-{{ sonata_admin.inline }} {% if errors|length > 0 %}sonata-ba-field-error{% endif %} {% if not has_label %}sonata-collection-row-without-label{% endif %}">
  174. {{ form_widget(form, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  175. {% if errors|length > 0 %}
  176. <div class="help-block sonata-ba-field-error-messages">
  177. {{ form_errors(form) }}
  178. </div>
  179. {% endif %}
  180. {% if sonata_admin.field_description.help %}
  181. <span class="help-block sonata-ba-field-help">{{ sonata_admin.admin.trans(sonata_admin.field_description.help, {}, sonata_admin.field_description.translationDomain)|raw }}</span>
  182. {% endif %}
  183. </div>
  184. </div>
  185. {% endif %}
  186. {% endblock form_row %}
  187. {% block sonata_type_native_collection_widget_row %}
  188. {% spaceless %}
  189. <div class="sonata-collection-row">
  190. {% if allow_delete %}
  191. <a href="#" class="btn sonata-collection-delete"><i class="fa fa-minus-circle"></i></a>
  192. {% endif %}
  193. {{ form_row(child) }}
  194. </div>
  195. {% endspaceless %}
  196. {% endblock sonata_type_native_collection_widget_row %}
  197. {% block sonata_type_native_collection_widget %}
  198. {% spaceless %}
  199. {% if prototype is defined %}
  200. {% set child = prototype %}
  201. {% set attr = attr|merge({'data-prototype': block('sonata_type_native_collection_widget_row'), 'data-prototype-name': prototype.vars.name, 'class': attr.class|default('') }) %}
  202. {% endif %}
  203. <div {{ block('widget_container_attributes') }}>
  204. {{ form_errors(form) }}
  205. {% for child in form %}
  206. {{ block('sonata_type_native_collection_widget_row') }}
  207. {% endfor %}
  208. {{ form_rest(form) }}
  209. {% if allow_add %}
  210. <div><a href="#" class="btn sonata-collection-add"><i class="fa fa-plus-circle"></i></a></div>
  211. {% endif %}
  212. </div>
  213. {% endspaceless %}
  214. {% endblock sonata_type_native_collection_widget %}
  215. {% block sonata_type_immutable_array_widget %}
  216. {% spaceless %}
  217. <div {{ block('widget_container_attributes') }}>
  218. {{ form_errors(form) }}
  219. {% for key, child in form %}
  220. {{ block('sonata_type_immutable_array_widget_row') }}
  221. {% endfor %}
  222. {{ form_rest(form) }}
  223. </div>
  224. {% endspaceless %}
  225. {% endblock sonata_type_immutable_array_widget %}
  226. {% block sonata_type_immutable_array_widget_row %}
  227. {% spaceless %}
  228. <div class="form-group{% if child.vars.errors|length > 0%} error{%endif%}" id="sonata-ba-field-container-{{ id }}-{{ key }}">
  229. {{ form_label(child) }}
  230. {% set div_class = "" %}
  231. {% if sonata_admin.admin and sonata_admin.admin.getConfigurationPool().getOption('form_type') == 'horizontal' %}
  232. {% set div_class = "col-sm-9 col-md-9" %}
  233. {% endif%}
  234. <div class="{{ div_class }} sonata-ba-field sonata-ba-field-{{ sonata_admin.edit }}-{{ sonata_admin.inline }} {% if child.vars.errors|length > 0 %}sonata-ba-field-error{% endif %}">
  235. {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  236. </div>
  237. {% if child.vars.errors|length > 0 %}
  238. <div class="help-block sonata-ba-field-error-messages">
  239. {{ form_errors(child) }}
  240. </div>
  241. {% endif %}
  242. </div>
  243. {% endspaceless %}
  244. {% endblock %}
  245. {% block sonata_type_model_autocomplete_widget %}
  246. {% include template %}
  247. {% endblock sonata_type_model_autocomplete_widget %}
  248. {% block sonata_type_choice_field_mask_widget %}
  249. {{ block('choice_widget') }}
  250. {% set main_form_name = id|slice(0, id|length - name|length) %}
  251. <script>
  252. jQuery(document).ready(function() {
  253. var allFields = {{ all_fields|json_encode|raw }};
  254. var map = {{ map|json_encode|raw }};
  255. showMaskChoiceEl = jQuery('#{{ main_form_name }}{{ name }}');
  256. showMaskChoiceEl.on('change', function () {
  257. choice_field_mask_show(jQuery(this).val());
  258. });
  259. function choice_field_mask_show(val)
  260. {
  261. var controlGroupIdFunc = function (field) {
  262. return '#sonata-ba-field-container-{{ main_form_name }}' + field;
  263. };
  264. if (map[val] == undefined) {
  265. jQuery.each(allFields, function (i, field) {
  266. jQuery(controlGroupIdFunc(field)).hide();
  267. });
  268. return;
  269. }
  270. jQuery.each(allFields, function (i, field) {
  271. jQuery(controlGroupIdFunc(field)).hide();
  272. });
  273. jQuery.each(map[val], function (i, field) {
  274. jQuery(controlGroupIdFunc(field)).show();
  275. });
  276. }
  277. choice_field_mask_show(showMaskChoiceEl.val());
  278. });
  279. </script>
  280. {% endblock %}
  281. {% block sonata_type_choice_multiple_sortable %}
  282. <input type="hidden" name="{{ full_name }}" id="{{ id }}" value="{{ value|join(',') }}" />
  283. <script>
  284. jQuery(document).ready(function() {
  285. var $target = jQuery('#{{ id }}');
  286. Admin.setup_sortable_select2($target, {{ form.vars.choices|json_encode|raw }});
  287. });
  288. </script>
  289. {% endblock %}