form_admin_fields.html.twig 13 KB

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