form_admin_fields.html.twig 17 KB

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