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.

126 lines
5.5KB

  1. {% form_theme form with easyadmin_config('design.form_theme') only %}
  2. {% set _entity_config = easyadmin_entity(app.request.query.get('entity')) %}
  3. {% trans_default_domain _entity_config.translation_domain %}
  4. {% set _trans_parameters = { '%entity_name%': _entity_config.name|trans, '%entity_label%': _entity_config.label|trans } %}
  5. {% extends _entity_config.templates.layout %}
  6. {% block body_id 'easyadmin-new-' ~ _entity_config.name %}
  7. {% block body_class 'new new-' ~ _entity_config.name|lower %}
  8. {% block content_title %}
  9. {% apply spaceless %}
  10. {% set _default_title = 'new.page_title'|trans(_trans_parameters, 'EasyAdminBundle') %}
  11. {{ _entity_config.new.title is defined ? _entity_config.new.title|trans(_trans_parameters) : _default_title }}
  12. {% endapply %}
  13. {% endblock %}
  14. {% block content_footer_wrapper '' %}
  15. {% block main %}
  16. {% block entity_form %}
  17. {{ form(form) }}
  18. {% endblock entity_form %}
  19. {% endblock %}
  20. {% block head_stylesheets %}
  21. {{ parent() }}
  22. <link rel="stylesheet" href="{{ asset('bundles/lcshop/css/backend/adminlte/plugins/jquery-ui/jquery-ui.min.css') }}">
  23. {% endblock %}
  24. {% block body_javascript %}
  25. {{ parent() }}
  26. <script src="{{ asset('bundles/lcshop/js/backend/plugin/jquery-ui/jquery-ui.min.js') }}"></script>
  27. {#
  28. <script type="text/javascript">
  29. $(function() {
  30. $('.new-form').areYouSure({ 'message': '{{ 'form.are_you_sure'|trans({}, 'EasyAdminBundle')|e('js') }}' });
  31. const entityForm = document.querySelector('form.new-form');
  32. const formSubmitButton = entityForm.querySelector('button[type="submit"]');
  33. const inputFieldsSelector = 'input,select,textarea';
  34. // Adding visual feedback for invalid fields: any ".form-group" with invalid fields
  35. // receives "has-error" class. The class is removed on click on the ".form-group"
  36. // itself to support custom/complex fields.
  37. formSubmitButton.addEventListener('click', function() {
  38. entityForm.querySelectorAll(inputFieldsSelector).forEach(function (input) {
  39. if (!input.validity.valid) {
  40. const formGroup = input.closest('div.form-group');
  41. formGroup.classList.add('has-error');
  42. formGroup.addEventListener('click', function onFormGroupClick() {
  43. formGroup.classList.remove('has-error');
  44. formGroup.removeEventListener('click', onFormGroupClick);
  45. });
  46. }
  47. });
  48. });
  49. // forms with tabs require some special treatment for errors. The problem
  50. // is when the field with errors is included in a tab not currently visible.
  51. // Browser shows this error "An invalid form control with name='...' is not focusable."
  52. // So, the user clicks on Submit button, the form is not submitted and the error
  53. // is not displayed. This JavaScript code ensures that each tab shows a badge with
  54. // the number of errors in it.
  55. formSubmitButton.addEventListener('click', function() {
  56. const formTabPanes = entityForm.querySelectorAll('.tab-pane');
  57. if (0 === formTabPanes.length) {
  58. return;
  59. }
  60. let firstNavTabItemWithError = null;
  61. formTabPanes.forEach(function (tabPane) {
  62. let tabPaneNumErrors = 0;
  63. tabPane.querySelectorAll(inputFieldsSelector).forEach(function (input) {
  64. if (!input.validity.valid) {
  65. tabPaneNumErrors++;
  66. }
  67. });
  68. let navTabItem = entityForm.querySelector('.nav-item a[href="#' + tabPane.id + '"]');
  69. let existingErrorBadge = navTabItem.querySelector('span.badge.badge-danger');
  70. if (null !== existingErrorBadge) {
  71. navTabItem.removeChild(existingErrorBadge);
  72. }
  73. if (tabPaneNumErrors > 0) {
  74. let newErrorBadge = document.createElement('span');
  75. newErrorBadge.classList.add('badge', 'badge-danger');
  76. newErrorBadge.title = 'form.tab.error_badge_title';
  77. newErrorBadge.textContent = tabPaneNumErrors;
  78. navTabItem.appendChild(newErrorBadge);
  79. if (null === firstNavTabItemWithError) {
  80. firstNavTabItemWithError = navTabItem;
  81. }
  82. }
  83. });
  84. if (firstNavTabItemWithError) {
  85. firstNavTabItemWithError.click();
  86. }
  87. });
  88. // prevent multiple form submissions to avoid creating duplicated entities
  89. entityForm.addEventListener('submit', function() {
  90. // this timeout is needed to include the disabled button into the submitted form
  91. setTimeout(function() {
  92. const submitButtons = entityForm.querySelectorAll('[type="submit"]');
  93. submitButtons.forEach(function(button) {
  94. button.setAttribute('disabled', 'disabled');
  95. });
  96. }, 1);
  97. }, false);
  98. });
  99. </script>
  100. #}
  101. {#{{ include('@EasyAdmin/default/includes/_select2_widget.html.twig') }}#}
  102. {% endblock %}