From 3e3c57afb8846fbfea37bb6eb5114433acceeaa0 Mon Sep 17 00:00:00 2001 From: Jan Kissig Date: Fri, 8 May 2026 07:59:22 +0000 Subject: [PATCH] Bug 42398: Reimplement form validation to news This patch reimplements form validation on news (additional contents). The form.validate() call which adds a submitHandler is moved outside the document ready block in order to work again. Markup like class=required is added to meet JS10 guidelines. JS alerts (when default title or default content was empty) were replaced by setting focus the required field. Test plan: a) go to /cgi-bin/koha/tools/additional-contents.pl?category=news (Tools -> News) b) click either 'New entry' or 'New entry using text editor' c) enter a required publication date d) save the form. You will not be required to enter a default title or content. e) apply patch followed by a restart_all f) go to /cgi-bin/koha/tools/additional-contents.pl?category=news (Tools -> News) g) click either 'New entry' or 'New entry using text editor' h) check that title and content in the 'Default' tab are marked as required i) try to send form f.e. when only title and content in 'English' tab are set j) check if default title or content is missing the default tab is shown and the field gets focused Signed-off-by: David Nind Signed-off-by: Jonathan Druart Signed-off-by: Lucas Gass (cherry picked from commit f4678e5b23908abd2ee9c1a7f6e82134b072c073) Signed-off-by: Jacob O'Mara --- .../en/modules/tools/additional-contents.tt | 74 +++++++++++++------ 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/additional-contents.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/additional-contents.tt index 68eea23180..324b3e0fd9 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/additional-contents.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/additional-contents.tt @@ -314,16 +314,35 @@ [% WRAPPER tab_panels %] [% FOR language IN languages %] [% SET translated_content = translated_contents.item(language.lang) %] + [% SET required = category == 'news' && language.lang == 'default' ? 1 : 0 %] [% WRAPPER tab_panel tabname="lang_${language.lang}" %]
  1. - - + + + [% IF required %]Required[% END %]
  2. - - @@ -690,33 +709,44 @@ $( "#" + location + "_notes" ).show(); } - $(document).ready(function() { - [% IF category == 'news' %] + const category = "[% category | html %]"; + const wysiwyg = [% wysiwyg ? 1 : 0 | html %]; + + if (category == "news") { $("#add_additional_content").validate({ - submitHandler: function(form){ - if ( ! $("#title_default").val().length > 0 ) { - alert(_("Please specify a title for 'Default'")); + submitHandler: function (form) { + // catch an empty title_default when default tab is not visible + if (!document.querySelector("#lang_default_panel.show") && !$("#title_default").val().length > 0) { + const defaultTab = document.querySelector("#lang_default-tab"); + const tab = new bootstrap.Tab(defaultTab); + tab.show(); + $("#title_default").focus(); return false; } - else { - [% UNLESS wysiwyg %] - let content = $('#content_default').siblings(".CodeMirror")[0].CodeMirror.getValue(); - [% ELSE %] - let content = tinyMCE.get('content_default').getContent(); - [% END %] - if ( ! content.length > 0 ) { - alert(_("Please specify a content for 'Default'")); - return false; + let content = wysiwyg ? tinyMCE.get("content_default").getContent() : $("#content_default").siblings(".CodeMirror")[0].CodeMirror.getValue(); + if (!content.length > 0) { + // catch an empty content_default when default tab is not visible + if (!document.querySelector("#lang_default_panel.show")) { + const defaultTab = document.querySelector("#lang_default-tab"); + const tab = new bootstrap.Tab(defaultTab); + tab.show(); } + // focus editor + if (wysiwyg) { + tinyMCE.get("content_default").focus(); + } else { + $("#content_default").siblings(".CodeMirror")[0].CodeMirror.focus(); + } + return false; } - form.submit(); - } + }, }); - [% END %] + } - showLocationNotes( $("#location").val() ); + $(document).ready(function () { + showLocationNotes($("#location").val()); $("#location").on("change", function(){ showLocationNotes( $(this).val() );