form_admin_fields.html.twig 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. {% block checkbox_widget -%}
  69. {% set parent_label_class = parent_label_class|default('') -%}
  70. {% if 'checkbox-inline' in parent_label_class %}
  71. {{- form_label(form, null, { widget: parent() }) -}}
  72. {% else -%}
  73. <div class="checkbox">
  74. {{- form_label(form, null, { widget: parent() }) -}}
  75. </div>
  76. {%- endif %}
  77. {%- endblock checkbox_widget %}
  78. {% block radio_widget -%}
  79. {%- set parent_label_class = parent_label_class|default('') -%}
  80. {% if 'radio-inline' in parent_label_class %}
  81. {{- form_label(form, null, { widget: parent() }) -}}
  82. {% else -%}
  83. <div class="radio">
  84. {{- form_label(form, null, { widget: parent() }) -}}
  85. </div>
  86. {%- endif %}
  87. {%- endblock radio_widget %}
  88. {# Labels #}
  89. {% block form_label %}
  90. {% spaceless %}
  91. {% if label is not same as(false) and sonata_admin.options['form_type'] == 'horizontal' %}
  92. {% set label_class = 'col-sm-3' %}
  93. {% endif %}
  94. {% set label_class = label_class|default('') ~ ' control-label' %}
  95. {% if label is not same as(false) %}
  96. {% set label_attr = label_attr|merge({'class': label_attr.class|default('') ~ label_class }) %}
  97. {% if not compound %}
  98. {% set label_attr = label_attr|merge({'for': id}) %}
  99. {% endif %}
  100. {% if required %}
  101. {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
  102. {% endif %}
  103. {% if label is empty %}
  104. {%- if label_format is defined and label_format is not empty -%}
  105. {% set label = label_format|replace({
  106. '%name%': name,
  107. '%id%': id,
  108. }) %}
  109. {%- else -%}
  110. {% set label = name|humanize %}
  111. {%- endif -%}
  112. {% endif %}
  113. <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
  114. {% if not sonata_admin.admin %}
  115. {{- label|trans({}, translation_domain) -}}
  116. {% else %}
  117. {{ sonata_admin.admin.trans(label, {}, sonata_admin.field_description.translationDomain) }}
  118. {% endif %}
  119. </label>
  120. {% endif %}
  121. {% endspaceless %}
  122. {% endblock form_label %}
  123. {% block checkbox_label -%}
  124. {{- block('checkbox_radio_label') -}}
  125. {%- endblock checkbox_label %}
  126. {% block radio_label -%}
  127. {{- block('checkbox_radio_label') -}}
  128. {%- endblock radio_label %}
  129. {% block checkbox_radio_label %}
  130. {% if sonata_admin.admin %}
  131. {% set translation_domain = sonata_admin.field_description.translationDomain %}
  132. {% endif %}
  133. {# Do not display the label if widget is not defined in order to prevent double label rendering #}
  134. {% if widget is defined %}
  135. {% if required %}
  136. {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) %}
  137. {% endif %}
  138. {% if parent_label_class is defined %}
  139. {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ parent_label_class)|trim}) %}
  140. {% endif %}
  141. {% if label is not same as(false) and label is empty %}
  142. {% set label = name|humanize %}
  143. {% endif %}
  144. <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
  145. {{- widget|raw -}}
  146. {%- if label is not same as(false) -%}
  147. <span class="control-label__text">
  148. {{- label|trans({}, translation_domain) -}}
  149. </span>
  150. {%- endif -%}
  151. </label>
  152. {% endif %}
  153. {% endblock checkbox_radio_label %}
  154. {% block choice_widget_expanded %}
  155. {% spaceless %}
  156. {% set attr = attr|merge({'class': attr.class|default('') ~ ' list-unstyled'}) %}
  157. <ul {{ block('widget_container_attributes') }}>
  158. {% for child in form %}
  159. <li>
  160. {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  161. </li>
  162. {% endfor %}
  163. </ul>
  164. {% endspaceless %}
  165. {% endblock choice_widget_expanded %}
  166. {% block choice_widget_collapsed %}
  167. {% spaceless %}
  168. {% if required and placeholder is defined and placeholder is none %}
  169. {% set required = false %}
  170. {% 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 %}
  171. {% set required = false %}
  172. {% endif %}
  173. {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  174. {% if (sortable is defined) and sortable and multiple %}
  175. {{ block('sonata_type_choice_multiple_sortable') }}
  176. {% else %}
  177. <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %} >
  178. {% if empty_value is defined and empty_value is not none %}
  179. <option value=""{% if required and value is empty %} selected="selected"{% endif %}>
  180. {% if not sonata_admin.admin %}
  181. {{- empty_value|trans({}, translation_domain) -}}
  182. {% else %}
  183. {{- empty_value|trans({}, sonata_admin.field_description.translationDomain) -}}
  184. {% endif%}
  185. </option>
  186. {% elseif placeholder is defined and placeholder is not none %}
  187. <option value=""{% if required and value is empty %} selected="selected"{% endif %}>
  188. {% if not sonata_admin.admin %}
  189. {{- placeholder|trans({}, translation_domain) -}}
  190. {% else %}
  191. {{- placeholder|trans({}, sonata_admin.field_description.translationDomain) -}}
  192. {% endif%}
  193. </option>
  194. {% endif %}
  195. {% if preferred_choices|length > 0 %}
  196. {% set options = preferred_choices %}
  197. {{ block('choice_widget_options') }}
  198. {% if choices|length > 0 %}
  199. <option disabled="disabled">{{ separator }}</option>
  200. {% endif %}
  201. {% endif %}
  202. {% set options = choices %}
  203. {{ block('choice_widget_options') }}
  204. </select>
  205. {% endif %}
  206. {% endspaceless %}
  207. {% endblock choice_widget_collapsed %}
  208. {% block date_widget %}
  209. {% spaceless %}
  210. {% if widget == 'single_text' %}
  211. {{ block('form_widget_simple') }}
  212. {% else %}
  213. {% if row is not defined or row == true %}
  214. {% set attr = attr|merge({'class': attr.class|default('') ~ ' row' }) %}
  215. {% endif %}
  216. {% set input_wrapper_class = input_wrapper_class|default('col-sm-4') %}
  217. <div {{ block('widget_container_attributes') }}>
  218. {{ date_pattern|replace({
  219. '{{ year }}': '<div class="'~ input_wrapper_class ~ '">' ~ form_widget(form.year) ~ '</div>',
  220. '{{ month }}': '<div class="'~ input_wrapper_class ~ '">' ~ form_widget(form.month) ~ '</div>',
  221. '{{ day }}': '<div class="'~ input_wrapper_class ~ '">' ~ form_widget(form.day) ~ '</div>',
  222. })|raw }}
  223. </div>
  224. {% endif %}
  225. {% endspaceless %}
  226. {% endblock date_widget %}
  227. {% block time_widget %}
  228. {% spaceless %}
  229. {% if widget == 'single_text' %}
  230. {{ block('form_widget_simple') }}
  231. {% else %}
  232. {% if row is not defined or row == true %}
  233. {% set attr = attr|merge({'class': attr.class|default('') ~ ' row' }) %}
  234. {% endif %}
  235. {% set input_wrapper_class = input_wrapper_class|default('col-sm-6') %}
  236. <div {{ block('widget_container_attributes') }}>
  237. <div class="{{ input_wrapper_class }}">
  238. {{ form_widget(form.hour) }}
  239. </div>
  240. {% if with_minutes %}
  241. <div class="{{ input_wrapper_class }}">
  242. {{ form_widget(form.minute) }}
  243. </div>
  244. {% endif %}
  245. {% if with_seconds %}
  246. <div class="{{ input_wrapper_class }}">
  247. {{ form_widget(form.second) }}
  248. </div>
  249. {% endif %}
  250. </div>
  251. {% endif %}
  252. {% endspaceless %}
  253. {% endblock time_widget %}
  254. {% block datetime_widget %}
  255. {% spaceless %}
  256. {% if widget == 'single_text' %}
  257. {{ block('form_widget_simple') }}
  258. {% else %}
  259. {% set attr = attr|merge({'class': attr.class|default('') ~ ' row' }) %}
  260. <div {{ block('widget_container_attributes') }}>
  261. {{ form_errors(form.date) }}
  262. {{ form_errors(form.time) }}
  263. {{ form_widget(form.date, {'row': false, 'input_wrapper_class': 'col-sm-2'}) }}
  264. {{ form_widget(form.time, {'row': false, 'input_wrapper_class': 'col-sm-2'}) }}
  265. </div>
  266. {% endif %}
  267. {% endspaceless %}
  268. {% endblock datetime_widget %}
  269. {% block form_row %}
  270. {% set show_label = show_label|default(true) %}
  271. <div class="form-group{% if errors|length > 0 %} has-error{% endif %}" id="sonata-ba-field-container-{{ id }}">
  272. {% if sonata_admin.field_description.options is defined %}
  273. {% set label = sonata_admin.field_description.options.name|default(label) %}
  274. {% endif %}
  275. {% set div_class = 'sonata-ba-field' %}
  276. {% if label is same as(false) %}
  277. {% set div_class = div_class ~ ' sonata-collection-row-without-label' %}
  278. {% endif %}
  279. {% if sonata_admin is defined and sonata_admin.options['form_type'] == 'horizontal' %}
  280. {% if label is same as(false) %}
  281. {% if 'collection' in form.parent.vars.block_prefixes %}
  282. {% set div_class = div_class ~ ' col-sm-12' %}
  283. {% else %}
  284. {% set div_class = div_class ~ ' col-sm-9 col-sm-offset-3' %}
  285. {% endif %}
  286. {% else %}
  287. {% set div_class = div_class ~ ' col-sm-9' %}
  288. {% endif %}
  289. {% endif %}
  290. {% if show_label %}
  291. {{ form_label(form, label|default(null)) }}
  292. {% endif %}
  293. {% if sonata_admin is defined and sonata_admin_enabled %}
  294. {% set div_class = div_class ~ ' sonata-ba-field-' ~ sonata_admin.edit ~ '-' ~ sonata_admin.inline %}
  295. {% endif %}
  296. {% if errors|length > 0 %}
  297. {% set div_class = div_class ~ ' sonata-ba-field-error' %}
  298. {% endif %}
  299. <div class="{{ div_class }}">
  300. {{ form_widget(form, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  301. {% if errors|length > 0 %}
  302. <div class="help-block sonata-ba-field-error-messages">
  303. {{ form_errors(form) }}
  304. </div>
  305. {% endif %}
  306. {% if sonata_admin is defined and sonata_admin_enabled and sonata_admin.field_description.help|default(false) %}
  307. <span class="help-block sonata-ba-field-help">{{ sonata_admin.admin.trans(sonata_admin.field_description.help, {}, sonata_admin.field_description.translationDomain)|raw }}</span>
  308. {% endif %}
  309. </div>
  310. </div>
  311. {% endblock form_row %}
  312. {% block checkbox_row -%}
  313. {% set show_label = false %}
  314. {{ block('form_row') }}
  315. {%- endblock checkbox_row %}
  316. {% block radio_row -%}
  317. {% set show_label = false %}
  318. {{ block('form_row') }}
  319. {%- endblock radio_row %}
  320. {% block sonata_type_native_collection_widget_row %}
  321. {% spaceless %}
  322. <div class="sonata-collection-row">
  323. {% if allow_delete %}
  324. <div class="row">
  325. <div class="col-xs-1">
  326. <a href="#" class="btn btn-link sonata-collection-delete">
  327. <i class="fa fa-minus-circle"></i>
  328. </a>
  329. </div>
  330. <div class="col-xs-11">
  331. {% endif %}
  332. {{ form_row(child, { label: false }) }}
  333. {% if allow_delete %}
  334. </div>
  335. </div>
  336. {% endif %}
  337. </div>
  338. {% endspaceless %}
  339. {% endblock sonata_type_native_collection_widget_row %}
  340. {% block sonata_type_native_collection_widget %}
  341. {% spaceless %}
  342. {% if prototype is defined %}
  343. {% set child = prototype %}
  344. {% set allow_delete_backup = allow_delete %}
  345. {% set allow_delete = true %}
  346. {% set attr = attr|merge({'data-prototype': block('sonata_type_native_collection_widget_row'), 'data-prototype-name': prototype.vars.name, 'class': attr.class|default('') }) %}
  347. {% set allow_delete = allow_delete_backup %}
  348. {% endif %}
  349. <div {{ block('widget_container_attributes') }}>
  350. {{ form_errors(form) }}
  351. {% for child in form %}
  352. {{ block('sonata_type_native_collection_widget_row') }}
  353. {% endfor %}
  354. {{ form_rest(form) }}
  355. {% if allow_add %}
  356. <div><a href="#" class="btn btn-link sonata-collection-add"><i class="fa fa-plus-circle"></i></a></div>
  357. {% endif %}
  358. </div>
  359. {% endspaceless %}
  360. {% endblock sonata_type_native_collection_widget %}
  361. {% block sonata_type_immutable_array_widget %}
  362. {% spaceless %}
  363. <div {{ block('widget_container_attributes') }}>
  364. {{ form_errors(form) }}
  365. {% for key, child in form %}
  366. {{ block('sonata_type_immutable_array_widget_row') }}
  367. {% endfor %}
  368. {{ form_rest(form) }}
  369. </div>
  370. {% endspaceless %}
  371. {% endblock sonata_type_immutable_array_widget %}
  372. {% block sonata_type_immutable_array_widget_row %}
  373. {% spaceless %}
  374. <div class="form-group{% if child.vars.errors|length > 0%} error{%endif%}" id="sonata-ba-field-container-{{ id }}-{{ key }}">
  375. {{ form_label(child) }}
  376. {% set div_class = "" %}
  377. {% if sonata_admin.options['form_type'] == 'horizontal' %}
  378. {% set div_class = 'col-sm-9' %}
  379. {% endif%}
  380. <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 %}">
  381. {{ form_widget(child, {'horizontal': false, 'horizontal_input_wrapper_class': ''}) }} {# {'horizontal': false, 'horizontal_input_wrapper_class': ''} needed to avoid MopaBootstrapBundle messing with the DOM #}
  382. </div>
  383. {% if child.vars.errors|length > 0 %}
  384. <div class="help-block sonata-ba-field-error-messages">
  385. {{ form_errors(child) }}
  386. </div>
  387. {% endif %}
  388. </div>
  389. {% endspaceless %}
  390. {% endblock %}
  391. {% block sonata_type_model_autocomplete_widget %}
  392. {% include template %}
  393. {% endblock sonata_type_model_autocomplete_widget %}
  394. {% block sonata_type_choice_field_mask_widget %}
  395. {{ block('choice_widget') }}
  396. {% set main_form_name = id|slice(0, id|length - name|length) %}
  397. <script>
  398. jQuery(document).ready(function() {
  399. var allFields = {{ all_fields|json_encode|raw }};
  400. var map = {{ map|json_encode|raw }};
  401. showMaskChoiceEl = jQuery('#{{ main_form_name }}{{ name }}');
  402. showMaskChoiceEl.on('change', function () {
  403. choice_field_mask_show(jQuery(this).val());
  404. });
  405. function choice_field_mask_show(val)
  406. {
  407. var controlGroupIdFunc = function (field) {
  408. return '#sonata-ba-field-container-{{ main_form_name }}' + field;
  409. };
  410. if (map[val] == undefined) {
  411. jQuery.each(allFields, function (i, field) {
  412. jQuery(controlGroupIdFunc(field)).hide();
  413. });
  414. return;
  415. }
  416. jQuery.each(allFields, function (i, field) {
  417. jQuery(controlGroupIdFunc(field)).hide();
  418. });
  419. jQuery.each(map[val], function (i, field) {
  420. jQuery(controlGroupIdFunc(field)).show();
  421. });
  422. }
  423. choice_field_mask_show(showMaskChoiceEl.val());
  424. });
  425. </script>
  426. {% endblock %}
  427. {% block sonata_type_choice_multiple_sortable %}
  428. <input type="hidden" name="{{ full_name }}" id="{{ id }}" value="{{ value|join(',') }}" />
  429. <script>
  430. jQuery(document).ready(function() {
  431. Admin.setup_sortable_select2(jQuery('#{{ id }}'), {{ form.vars.choices|json_encode|raw }});
  432. });
  433. </script>
  434. {% endblock %}