You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 3 година
пре 3 година
пре 3 година
пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. {# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
  2. {# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
  3. {% extends ea.templatePath('layout') %}
  4. {% form_theme new_form with ea.crud.formThemes only %}
  5. {% trans_default_domain ea.i18n.translationDomain %}
  6. {% block body_id 'ea-new-' ~ entity.name ~ '-' ~ entity.primaryKeyValue %}
  7. {% block body_class 'ea-new ea-new-' ~ entity.name %}
  8. {% block configured_head_contents %}
  9. {{ parent() }}
  10. {% for htmlContent in new_form.vars.ea_crud_form.assets.headContents %}
  11. {{ htmlContent|raw }}
  12. {% endfor %}
  13. {% endblock %}
  14. {% block configured_stylesheets %}
  15. {{ parent() }}
  16. {% for css_asset in new_form.vars.ea_crud_form.assets.cssFiles %}
  17. <link rel="stylesheet" href="{{ asset(css_asset) }}">
  18. {% endfor %}
  19. {% for webpack_encore_entry in new_form.vars.ea_crud_form.assets.webpackEncoreEntries %}
  20. {{ ea_call_function_if_exists('encore_entry_link_tags', webpack_encore_entry) }}
  21. {% endfor %}
  22. {% endblock %}
  23. {% block configured_javascripts %}
  24. {{ parent() }}
  25. {% for js_asset in new_form.vars.ea_crud_form.assets.jsFiles %}
  26. <script src="{{ asset(js_asset) }}"></script>
  27. {% endfor %}
  28. {% for webpack_encore_entry in new_form.vars.ea_crud_form.assets.webpackEncoreEntries %}
  29. {{ ea_call_function_if_exists('encore_entry_script_tags', webpack_encore_entry) }}
  30. {% endfor %}
  31. {% endblock %}
  32. {% block content_title %}
  33. {%- apply spaceless -%}
  34. {% set default_title = ea.crud.defaultPageTitle('new')|trans(ea.i18n.translationParameters, 'EasyAdminBundle') %}
  35. {{ ea.crud.customPageTitle is null ? default_title|raw : ea.crud.customPageTitle('new')|trans(ea.i18n.translationParameters)|raw }}
  36. {%- endapply -%}
  37. {% endblock %}
  38. {% block page_actions %}
  39. {% for action in entity.actions %}
  40. {{ include(action.templatePath, { action: action }, with_context = false) }}
  41. {% endfor %}
  42. {% endblock %}
  43. {% block main %}
  44. {% block new_form %}
  45. {{ form(new_form) }}
  46. {% endblock new_form %}
  47. {% endblock %}
  48. {% block body_javascript %}
  49. {{ parent() }}
  50. <script type="text/javascript">
  51. $(function() {
  52. $('.ea-new-form').areYouSure({ 'message': '{{ 'form.are_you_sure'|trans({}, 'EasyAdminBundle')|e('js') }}' });
  53. const entityForm = document.querySelector('form.ea-new-form');
  54. const inputFieldsSelector = 'input,select,textarea';
  55. // Adding visual feedback for invalid fields: any ".form-group" with invalid fields
  56. // receives "has-error" class. The class is removed on click on the ".form-group"
  57. // itself to support custom/complex fields.
  58. entityForm.addEventListener('submit', function(submitEvent) {
  59. entityForm.querySelectorAll(inputFieldsSelector).forEach(function (input) {
  60. if (!input.validity.valid) {
  61. const formGroup = input.closest('div.form-group');
  62. formGroup.classList.add('has-error');
  63. formGroup.addEventListener('click', function onFormGroupClick() {
  64. formGroup.classList.remove('has-error');
  65. formGroup.removeEventListener('click', onFormGroupClick);
  66. });
  67. }
  68. });
  69. const eaEvent = new CustomEvent('ea.form.submit', {
  70. cancelable: true,
  71. detail: { page: 'new', form: entityForm }
  72. });
  73. const eaEventResult = document.dispatchEvent(eaEvent);
  74. if (false === eaEventResult) {
  75. submitEvent.preventDefault();
  76. submitEvent.stopPropagation();
  77. }
  78. });
  79. // forms with tabs require some special treatment for errors. The problem
  80. // is when the field with errors is included in a tab not currently visible.
  81. // Browser shows this error "An invalid form control with name='...' is not focusable."
  82. // So, the user clicks on Submit button, the form is not submitted and the error
  83. // is not displayed. This JavaScript code ensures that each tab shows a badge with
  84. // the number of errors in it.
  85. entityForm.addEventListener('submit', function() {
  86. const formTabPanes = entityForm.querySelectorAll('.tab-pane');
  87. if (0 === formTabPanes.length) {
  88. return;
  89. }
  90. let firstNavTabItemWithError = null;
  91. formTabPanes.forEach(function (tabPane) {
  92. let tabPaneNumErrors = 0;
  93. tabPane.querySelectorAll(inputFieldsSelector).forEach(function (input) {
  94. if (!input.validity.valid) {
  95. tabPaneNumErrors++;
  96. }
  97. });
  98. let navTabItem = entityForm.querySelector('.nav-item a[href="#' + tabPane.id + '"]');
  99. let existingErrorBadge = navTabItem.querySelector('span.badge.badge-danger');
  100. if (null !== existingErrorBadge) {
  101. navTabItem.removeChild(existingErrorBadge);
  102. }
  103. if (tabPaneNumErrors > 0) {
  104. let newErrorBadge = document.createElement('span');
  105. newErrorBadge.classList.add('badge', 'badge-danger');
  106. newErrorBadge.title = 'form.tab.error_badge_title';
  107. newErrorBadge.textContent = tabPaneNumErrors;
  108. navTabItem.appendChild(newErrorBadge);
  109. if (null === firstNavTabItemWithError) {
  110. firstNavTabItemWithError = navTabItem;
  111. }
  112. }
  113. });
  114. if (firstNavTabItemWithError) {
  115. firstNavTabItemWithError.click();
  116. }
  117. });
  118. // prevent multiple form submissions to avoid creating duplicated entities
  119. entityForm.addEventListener('submit', function() {
  120. // this timeout is needed to include the disabled button into the submitted form
  121. setTimeout(function() {
  122. const submitButtons = entityForm.querySelectorAll('[type="submit"]');
  123. submitButtons.forEach(function(button) {
  124. button.setAttribute('disabled', 'disabled');
  125. });
  126. }, 1);
  127. }, false);
  128. });
  129. </script>
  130. {{ include('@EasyAdmin/crud/includes/_select2_widget.html.twig') }}
  131. {% endblock %}