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.

edit.html.twig 5.6KB

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