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.

118 lines
5.2KB

  1. {# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
  2. {# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
  3. {% set form = new_form %}
  4. {% extends '@LcSov/adminlte/crud/_form.html.twig' %}
  5. {% block body_id 'ea-new-' ~ entity.name ~ '-' ~ entity.primaryKeyValue %}
  6. {% block body_class 'ea-new ea-new-' ~ entity.name %}
  7. {% block content_title %}
  8. {%- apply spaceless -%}
  9. {% set default_title = ea.crud.defaultPageTitle('new')|trans(ea.i18n.translationParameters, 'EasyAdminBundle') %}
  10. {{ ea.crud.customPageTitle is null ? default_title|raw : ea.crud.customPageTitle('new')|trans(ea.i18n.translationParameters)|raw }}
  11. {%- endapply -%}
  12. {% endblock %}
  13. {% block body_javascript %}
  14. {{ parent() }}
  15. {# <script type="text/javascript">
  16. $(function () {
  17. $('.ea-new-form').areYouSure({'message': '{{ 'form.are_you_sure'|trans({}, 'EasyAdminBundle')|e('js') }}'});
  18. const entityForm = document.querySelector('form.ea-new-form');
  19. const inputFieldsSelector = 'input,select,textarea';
  20. // Adding visual feedback for invalid fields: any ".form-group" with invalid fields
  21. // receives "has-error" class. The class is removed on click on the ".form-group"
  22. // itself to support custom/complex fields.
  23. entityForm.addEventListener('submit', function (submitEvent) {
  24. entityForm.querySelectorAll(inputFieldsSelector).forEach(function (input) {
  25. if (!input.validity.valid) {
  26. const formGroup = input.closest('div.form-group');
  27. formGroup.classList.add('has-error');
  28. formGroup.addEventListener('click', function onFormGroupClick() {
  29. formGroup.classList.remove('has-error');
  30. formGroup.removeEventListener('click', onFormGroupClick);
  31. });
  32. }
  33. });
  34. const eaEvent = new CustomEvent('ea.form.submit', {
  35. cancelable: true,
  36. detail: {page: 'new', form: entityForm}
  37. });
  38. const eaEventResult = document.dispatchEvent(eaEvent);
  39. if (false === eaEventResult) {
  40. submitEvent.preventDefault();
  41. submitEvent.stopPropagation();
  42. }
  43. });
  44. // forms with tabs require some special treatment for errors. The problem
  45. // is when the field with errors is included in a tab not currently visible.
  46. // Browser shows this error "An invalid form control with name='...' is not focusable."
  47. // So, the user clicks on Submit button, the form is not submitted and the error
  48. // is not displayed. This JavaScript code ensures that each tab shows a badge with
  49. // the number of errors in it.
  50. entityForm.addEventListener('submit', function () {
  51. const formTabPanes = entityForm.querySelectorAll('.tab-pane');
  52. if (0 === formTabPanes.length) {
  53. return;
  54. }
  55. let firstNavTabItemWithError = null;
  56. formTabPanes.forEach(function (tabPane) {
  57. let tabPaneNumErrors = 0;
  58. tabPane.querySelectorAll(inputFieldsSelector).forEach(function (input) {
  59. if (!input.validity.valid) {
  60. tabPaneNumErrors++;
  61. }
  62. });
  63. let navTabItem = entityForm.querySelector('.nav-item a[href="#' + tabPane.id + '"]');
  64. let existingErrorBadge = navTabItem.querySelector('span.badge.badge-danger');
  65. if (null !== existingErrorBadge) {
  66. navTabItem.removeChild(existingErrorBadge);
  67. }
  68. if (tabPaneNumErrors > 0) {
  69. let newErrorBadge = document.createElement('span');
  70. newErrorBadge.classList.add('badge', 'badge-danger');
  71. newErrorBadge.title = 'form.tab.error_badge_title';
  72. newErrorBadge.textContent = tabPaneNumErrors;
  73. navTabItem.appendChild(newErrorBadge);
  74. if (null === firstNavTabItemWithError) {
  75. firstNavTabItemWithError = navTabItem;
  76. }
  77. }
  78. });
  79. if (firstNavTabItemWithError) {
  80. firstNavTabItemWithError.click();
  81. }
  82. });
  83. // prevent multiple form submissions to avoid creating duplicated entities
  84. entityForm.addEventListener('submit', function () {
  85. // this timeout is needed to include the disabled button into the submitted form
  86. setTimeout(function () {
  87. const submitButtons = entityForm.querySelectorAll('[type="submit"]');
  88. submitButtons.forEach(function (button) {
  89. button.setAttribute('disabled', 'disabled');
  90. });
  91. }, 1);
  92. }, false);
  93. });
  94. </script>
  95. {{ include('@EasyAdmin/crud/includes/_select2_widget.html.twig') }} #}
  96. {% endblock %}