form_admin_fields.html.twig 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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_errors -%}
  9. {% if errors|length > 0 %}
  10. {% if not form.parent %}<div class="alert alert-danger">{% endif %}
  11. <ul class="list-unstyled">
  12. {% for error in errors %}
  13. <li><i class="fa fa-exclamation-circle"></i> {{ error.message }}</li>
  14. {% endfor %}
  15. </ul>
  16. {% if not form.parent %}</div>{% endif %}
  17. {% endif %}
  18. {%- endblock form_errors %}
  19. {% block sonata_help %}
  20. {% spaceless %}
  21. {% if sonata_help is defined and sonata_help %}
  22. <span class="help-block sonata-ba-field-widget-help">{{ sonata_help|raw }}</span>
  23. {% endif %}
  24. {% endspaceless %}
  25. {% endblock %}
  26. {% block form_widget -%}
  27. {{ parent() }}
  28. {{ block('sonata_help') }}
  29. {%- endblock form_widget %}
  30. {% block form_widget_simple %}
  31. {% set type = type|default('text') %}
  32. {% if type != 'file' %}
  33. {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  34. {% endif %}
  35. {{ parent() }}
  36. {% endblock form_widget_simple %}
  37. {% block textarea_widget %}
  38. {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  39. {{ parent() }}
  40. {% endblock textarea_widget %}
  41. {% block money_widget -%}
  42. {% if money_pattern == '{{ widget }}' %}
  43. {{- block('form_widget_simple') -}}
  44. {% else %}
  45. {% set currencySymbol = money_pattern|replace({'{{ widget }}': ''})|trim %}
  46. {% if money_pattern matches '/^{{ widget }}/' %}
  47. <div class="input-group">
  48. {{- block('form_widget_simple') -}}
  49. <span class="input-group-addon">{{ currencySymbol }}</span>
  50. </div>
  51. {% elseif money_pattern matches '/{{ widget }}$/' %}
  52. <div class="input-group">
  53. <span class="input-group-addon">{{ currencySymbol }}</span>
  54. {{- block('form_widget_simple') -}}
  55. </div>
  56. {% endif %}
  57. {% endif %}
  58. {%- endblock money_widget %}
  59. {% block percent_widget %}
  60. {% spaceless %}
  61. {% set type = type|default('text') %}
  62. <div class="input-group">
  63. {{ block('form_widget_simple') }}
  64. <span class="input-group-addon">%</span>
  65. </div>
  66. {% endspaceless %}
  67. {% endblock percent_widget %}
  68. {# Labels #}
  69. {% block form_label %}
  70. {% spaceless %}
  71. {% if label is not same as(false) and sonata_admin.options['form_type'] == 'horizontal' %}
  72. {% set label_class = 'col-sm-3' %}
  73. {% endif %}
  74. {% set label_class = label_class|default('') ~ ' control-label' %}
  75. {% if label is not same as(false) %}
  76. {% set label_attr = label_attr|merge({'class': label_attr.class|default('') ~ label_class }) %}
  77. {% if not compound %}
  78. {% set label_attr = label_attr|merge({'for': id}) %}
  79. {% endif %}
  80. {% if required %}
  81. {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
  82. {% endif %}
  83. {% if label is empty %}
  84. {%- if label_format is defined and label_format is not empty -%}
  85. {% set label = label_format|replace({
  86. '%name%': name,
  87. '%id%': id,
  88. }) %}
  89. {%- else -%}
  90. {% set label = name|humanize %}
  91. {%- endif -%}
  92. {% endif %}
  93. {% if in_list_checkbox is defined and in_list_checkbox and widget is defined %}
  94. <label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  95. {{ widget|raw }}
  96. <span>
  97. {% if not sonata_admin.admin %}
  98. {{- label|trans({}, translation_domain) -}}
  99. {% else %}
  100. {{- label|trans({}, sonata_admin.field_description.translationDomain) -}}
  101. {% endif%}
  102. </span>
  103. </label>
  104. {% else %}
  105. <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
  106. {% if not sonata_admin.admin%}
  107. {{- label|trans({}, translation_domain) -}}
  108. {% else %}
  109. {{ sonata_admin.admin.trans(label, {}, sonata_admin.field_description.translationDomain) }}
  110. {% endif %}
  111. </label>
  112. {% endif %}
  113. {% endif %}
  114. {% endspaceless %}
  115. {% endblock form_label %}
  116. {% block choice_widget_expanded %}
  117. {% spaceless %}
  118. {% set attr = attr|merge({'class': attr.class|default('') ~ ' list-unstyled'}) %}
  119. <ul {{ block('widget_container_attributes') }}>
  120. {% for child in form %}
  121. <li>
  122. {% set form_widget_content %}
  123. {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  124. {% endset %}
  125. {{ form_label(child, child.vars.label|default(null), { 'in_list_checkbox' : true, 'widget' : form_widget_content } ) }}
  126. </li>
  127. {% endfor %}
  128. </ul>
  129. {% endspaceless %}
  130. {% endblock choice_widget_expanded %}
  131. {% block choice_widget_collapsed %}
  132. {% spaceless %}
  133. {% if required and placeholder is defined and placeholder is none %}
  134. {% set required = false %}
  135. {% elseif required and empty_value is defined and empty_value_in_choices is defined and empty_value is none and not empty_value_in_choices and not multiple %}
  136. {% set required = false %}
  137. {% endif %}
  138. {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  139. {% if (sortable is defined) and sortable and multiple %}
  140. {{ block('sonata_type_choice_multiple_sortable') }}
  141. {% else %}
  142. <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %} >
  143. {% if empty_value is defined and empty_value is not none %}
  144. <option value=""{% if required and value is empty %} selected="selected"{% endif %}>
  145. {% if not sonata_admin.admin %}
  146. {{- empty_value|trans({}, translation_domain) -}}
  147. {% else %}
  148. {{- empty_value|trans({}, sonata_admin.field_description.translationDomain) -}}
  149. {% endif%}
  150. </option>
  151. {% elseif placeholder is defined and placeholder is not none %}
  152. <option value=""{% if required and value is empty %} selected="selected"{% endif %}>
  153. {% if not sonata_admin.admin %}
  154. {{- placeholder|trans({}, translation_domain) -}}
  155. {% else %}
  156. {{- placeholder|trans({}, sonata_admin.field_description.translationDomain) -}}
  157. {% endif%}
  158. </option>
  159. {% endif %}
  160. {% if preferred_choices|length > 0 %}
  161. {% set options = preferred_choices %}
  162. {{ block('choice_widget_options') }}
  163. {% if choices|length > 0 %}
  164. <option disabled="disabled">{{ separator }}</option>
  165. {% endif %}
  166. {% endif %}
  167. {% set options = choices %}
  168. {{ block('choice_widget_options') }}
  169. </select>
  170. {% endif %}
  171. {% endspaceless %}
  172. {% endblock choice_widget_collapsed %}
  173. {% block date_widget %}
  174. {% spaceless %}
  175. {% if widget == 'single_text' %}
  176. {{ block('form_widget_simple') }}
  177. {% else %}
  178. {% if row is not defined or row == true %}
  179. {% set attr = attr|merge({'class': attr.class|default('') ~ ' row' }) %}
  180. {% endif %}
  181. {% set input_wrapper_class = input_wrapper_class|default('col-sm-4') %}
  182. <div {{ block('widget_container_attributes') }}>
  183. {{ date_pattern|replace({
  184. '{{ year }}': '<div class="'~ input_wrapper_class ~ '">' ~ form_widget(form.year) ~ '</div>',
  185. '{{ month }}': '<div class="'~ input_wrapper_class ~ '">' ~ form_widget(form.month) ~ '</div>',
  186. '{{ day }}': '<div class="'~ input_wrapper_class ~ '">' ~ form_widget(form.day) ~ '</div>',
  187. })|raw }}
  188. </div>
  189. {% endif %}
  190. {% endspaceless %}
  191. {% endblock date_widget %}
  192. {% block time_widget %}
  193. {% spaceless %}
  194. {% if widget == 'single_text' %}
  195. {{ block('form_widget_simple') }}
  196. {% else %}
  197. {% if row is not defined or row == true %}
  198. {% set attr = attr|merge({'class': attr.class|default('') ~ ' row' }) %}
  199. {% endif %}
  200. {% set input_wrapper_class = input_wrapper_class|default('col-sm-6') %}
  201. <div {{ block('widget_container_attributes') }}>
  202. <div class="{{ input_wrapper_class }}">
  203. {{ form_widget(form.hour) }}
  204. </div>
  205. {% if with_minutes %}
  206. <div class="{{ input_wrapper_class }}">
  207. {{ form_widget(form.minute) }}
  208. </div>
  209. {% endif %}
  210. {% if with_seconds %}
  211. <div class="{{ input_wrapper_class }}">
  212. {{ form_widget(form.second) }}
  213. </div>
  214. {% endif %}
  215. </div>
  216. {% endif %}
  217. {% endspaceless %}
  218. {% endblock time_widget %}
  219. {% block datetime_widget %}
  220. {% spaceless %}
  221. {% if widget == 'single_text' %}
  222. {{ block('form_widget_simple') }}
  223. {% else %}
  224. {% set attr = attr|merge({'class': attr.class|default('') ~ ' row' }) %}
  225. <div {{ block('widget_container_attributes') }}>
  226. {{ form_errors(form.date) }}
  227. {{ form_errors(form.time) }}
  228. {{ form_widget(form.date, {'row': false, 'input_wrapper_class': 'col-sm-2'}) }}
  229. {{ form_widget(form.time, {'row': false, 'input_wrapper_class': 'col-sm-2'}) }}
  230. </div>
  231. {% endif %}
  232. {% endspaceless %}
  233. {% endblock datetime_widget %}
  234. {% block form_row %}
  235. <div class="form-group{% if errors|length > 0 %} has-error{% endif %}" id="sonata-ba-field-container-{{ id }}">
  236. {% if sonata_admin.field_description.options is defined %}
  237. {% set label = sonata_admin.field_description.options.name|default(label) %}
  238. {% endif %}
  239. {% set div_class = 'sonata-ba-field' %}
  240. {% if label is same as(false) %}
  241. {% set div_class = div_class ~ ' sonata-collection-row-without-label' %}
  242. {% endif %}
  243. {% if sonata_admin is defined and sonata_admin.options['form_type'] == 'horizontal' %}
  244. {% if label is same as(false) %}
  245. {% if 'collection' in form.parent.vars.block_prefixes %}
  246. {% set div_class = div_class ~ ' col-sm-12' %}
  247. {% else %}
  248. {% set div_class = div_class ~ ' col-sm-9 col-sm-offset-3' %}
  249. {% endif %}
  250. {% else %}
  251. {% set div_class = div_class ~ ' col-sm-9' %}
  252. {% endif %}
  253. {% endif %}
  254. {{ form_label(form, label|default(null)) }}
  255. {% if sonata_admin is defined and sonata_admin_enabled %}
  256. {% set div_class = div_class ~ ' sonata-ba-field-' ~ sonata_admin.edit ~ '-' ~ sonata_admin.inline %}
  257. {% endif %}
  258. {% if errors|length > 0 %}
  259. {% set div_class = div_class ~ ' sonata-ba-field-error' %}
  260. {% endif %}
  261. <div class="{{ div_class }}">
  262. {{ form_widget(form, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  263. {% if errors|length > 0 %}
  264. <div class="help-block sonata-ba-field-error-messages">
  265. {{ form_errors(form) }}
  266. </div>
  267. {% endif %}
  268. {% if sonata_admin is defined and sonata_admin_enabled and sonata_admin.field_description.help|default(false) %}
  269. <span class="help-block sonata-ba-field-help">{{ sonata_admin.admin.trans(sonata_admin.field_description.help, {}, sonata_admin.field_description.translationDomain)|raw }}</span>
  270. {% endif %}
  271. </div>
  272. </div>
  273. {% endblock form_row %}
  274. {% block sonata_type_native_collection_widget_row %}
  275. {% spaceless %}
  276. <div class="sonata-collection-row">
  277. {% if allow_delete %}
  278. <div class="row">
  279. <div class="col-xs-1">
  280. <a href="#" class="btn btn-link sonata-collection-delete">
  281. <i class="fa fa-minus-circle"></i>
  282. </a>
  283. </div>
  284. <div class="col-xs-11">
  285. {% endif %}
  286. {{ form_row(child, { label: false }) }}
  287. {% if allow_delete %}
  288. </div>
  289. </div>
  290. {% endif %}
  291. </div>
  292. {% endspaceless %}
  293. {% endblock sonata_type_native_collection_widget_row %}
  294. {% block sonata_type_native_collection_widget %}
  295. {% spaceless %}
  296. {% if prototype is defined %}
  297. {% set child = prototype %}
  298. {% set allow_delete_backup = allow_delete %}
  299. {% set allow_delete = true %}
  300. {% set attr = attr|merge({'data-prototype': block('sonata_type_native_collection_widget_row'), 'data-prototype-name': prototype.vars.name, 'class': attr.class|default('') }) %}
  301. {% set allow_delete = allow_delete_backup %}
  302. {% endif %}
  303. <div {{ block('widget_container_attributes') }}>
  304. {{ form_errors(form) }}
  305. {% for child in form %}
  306. {{ block('sonata_type_native_collection_widget_row') }}
  307. {% endfor %}
  308. {{ form_rest(form) }}
  309. {% if allow_add %}
  310. <div><a href="#" class="btn btn-link sonata-collection-add"><i class="fa fa-plus-circle"></i></a></div>
  311. {% endif %}
  312. </div>
  313. {% endspaceless %}
  314. {% endblock sonata_type_native_collection_widget %}
  315. {% block sonata_type_immutable_array_widget %}
  316. {% spaceless %}
  317. <div {{ block('widget_container_attributes') }}>
  318. {{ form_errors(form) }}
  319. {% for key, child in form %}
  320. {{ block('sonata_type_immutable_array_widget_row') }}
  321. {% endfor %}
  322. {{ form_rest(form) }}
  323. </div>
  324. {% endspaceless %}
  325. {% endblock sonata_type_immutable_array_widget %}
  326. {% block sonata_type_immutable_array_widget_row %}
  327. {% spaceless %}
  328. <div class="form-group{% if child.vars.errors|length > 0%} error{%endif%}" id="sonata-ba-field-container-{{ id }}-{{ key }}">
  329. {{ form_label(child) }}
  330. {% set div_class = "" %}
  331. {% if sonata_admin.options['form_type'] == 'horizontal' %}
  332. {% set div_class = 'col-sm-9' %}
  333. {% endif%}
  334. <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 %}">
  335. {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  336. </div>
  337. {% if child.vars.errors|length > 0 %}
  338. <div class="help-block sonata-ba-field-error-messages">
  339. {{ form_errors(child) }}
  340. </div>
  341. {% endif %}
  342. </div>
  343. {% endspaceless %}
  344. {% endblock %}
  345. {% block sonata_type_model_autocomplete_widget %}
  346. {% include template %}
  347. {% endblock sonata_type_model_autocomplete_widget %}
  348. {% block sonata_type_choice_field_mask_widget %}
  349. {{ block('choice_widget') }}
  350. {% set main_form_name = id|slice(0, id|length - name|length) %}
  351. <script>
  352. jQuery(document).ready(function() {
  353. var allFields = {{ all_fields|json_encode|raw }};
  354. var map = {{ map|json_encode|raw }};
  355. showMaskChoiceEl = jQuery('#{{ main_form_name }}{{ name }}');
  356. showMaskChoiceEl.on('change', function () {
  357. choice_field_mask_show(jQuery(this).val());
  358. });
  359. function choice_field_mask_show(val)
  360. {
  361. var controlGroupIdFunc = function (field) {
  362. return '#sonata-ba-field-container-{{ main_form_name }}' + field;
  363. };
  364. if (map[val] == undefined) {
  365. jQuery.each(allFields, function (i, field) {
  366. jQuery(controlGroupIdFunc(field)).hide();
  367. });
  368. return;
  369. }
  370. jQuery.each(allFields, function (i, field) {
  371. jQuery(controlGroupIdFunc(field)).hide();
  372. });
  373. jQuery.each(map[val], function (i, field) {
  374. jQuery(controlGroupIdFunc(field)).show();
  375. });
  376. }
  377. choice_field_mask_show(showMaskChoiceEl.val());
  378. });
  379. </script>
  380. {% endblock %}
  381. {% block sonata_type_choice_multiple_sortable %}
  382. <input type="hidden" name="{{ full_name }}" id="{{ id }}" value="{{ value|join(',') }}" />
  383. <script>
  384. jQuery(document).ready(function() {
  385. Admin.setup_sortable_select2(jQuery('#{{ id }}'), {{ form.vars.choices|json_encode|raw }});
  386. });
  387. </script>
  388. {% endblock %}