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.

new.html.twig 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 body_javascript %}
  21. {{ parent() }}
  22. <script type="text/javascript">
  23. $(function() {
  24. $('.new-form').areYouSure({ 'message': '{{ 'form.are_you_sure'|trans({}, 'EasyAdminBundle')|e('js') }}' });
  25. const entityForm = document.querySelector('form.new-form');
  26. const formSubmitButton = entityForm.querySelector('button[type="submit"]');
  27. const inputFieldsSelector = 'input,select,textarea';
  28. // Adding visual feedback for invalid fields: any ".form-group" with invalid fields
  29. // receives "has-error" class. The class is removed on click on the ".form-group"
  30. // itself to support custom/complex fields.
  31. formSubmitButton.addEventListener('click', function() {
  32. entityForm.querySelectorAll(inputFieldsSelector).forEach(function (input) {
  33. if (!input.validity.valid) {
  34. const formGroup = input.closest('div.form-group');
  35. formGroup.classList.add('has-error');
  36. formGroup.addEventListener('click', function onFormGroupClick() {
  37. formGroup.classList.remove('has-error');
  38. formGroup.removeEventListener('click', onFormGroupClick);
  39. });
  40. }
  41. });
  42. });
  43. // forms with tabs require some special treatment for errors. The problem
  44. // is when the field with errors is included in a tab not currently visible.
  45. // Browser shows this error "An invalid form control with name='...' is not focusable."
  46. // So, the user clicks on Submit button, the form is not submitted and the error
  47. // is not displayed. This JavaScript code ensures that each tab shows a badge with
  48. // the number of errors in it.
  49. formSubmitButton.addEventListener('click', function() {
  50. const formTabPanes = entityForm.querySelectorAll('.tab-pane');
  51. if (0 === formTabPanes.length) {
  52. return;
  53. }
  54. let firstNavTabItemWithError = null;
  55. formTabPanes.forEach(function (tabPane) {
  56. let tabPaneNumErrors = 0;
  57. tabPane.querySelectorAll(inputFieldsSelector).forEach(function (input) {
  58. if (!input.validity.valid) {
  59. tabPaneNumErrors++;
  60. }
  61. });
  62. let navTabItem = entityForm.querySelector('.nav-item a[href="#' + tabPane.id + '"]');
  63. let existingErrorBadge = navTabItem.querySelector('span.badge.badge-danger');
  64. if (null !== existingErrorBadge) {
  65. navTabItem.removeChild(existingErrorBadge);
  66. }
  67. if (tabPaneNumErrors > 0) {
  68. let newErrorBadge = document.createElement('span');
  69. newErrorBadge.classList.add('badge', 'badge-danger');
  70. newErrorBadge.title = 'form.tab.error_badge_title';
  71. newErrorBadge.textContent = tabPaneNumErrors;
  72. navTabItem.appendChild(newErrorBadge);
  73. if (null === firstNavTabItemWithError) {
  74. firstNavTabItemWithError = navTabItem;
  75. }
  76. }
  77. });
  78. if (firstNavTabItemWithError) {
  79. firstNavTabItemWithError.click();
  80. }
  81. });
  82. // prevent multiple form submissions to avoid creating duplicated entities
  83. entityForm.addEventListener('submit', function() {
  84. // this timeout is needed to include the disabled button into the submitted form
  85. setTimeout(function() {
  86. const submitButtons = entityForm.querySelectorAll('[type="submit"]');
  87. submitButtons.forEach(function(button) {
  88. button.setAttribute('disabled', 'disabled');
  89. });
  90. }, 1);
  91. }, false);
  92. });
  93. </script>
  94. {{ include('@EasyAdmin/default/includes/_select2_widget.html.twig') }}
  95. {% endblock %}