From 23b571a3b025bda85fd309d7f0915d571781f424 Mon Sep 17 00:00:00 2001 From: Leo Stoyanov Date: Fri, 10 Jan 2025 23:01:17 +0000 Subject: [PATCH] Bug 38839: Add validation for replacement price in item and batch modification forms Adds validation for the replacement price on the item input and batch modification forms. This new patch utilizes the standard jQuery "decimal" validator to check if monetary input is appropriatly formatted. Test plan: 1. Search for a record in the staff interface, select any record with an item, click "Edit", and select "Manage items". 2. Click "Actions" on the item in the table, select "Edit", and input an invalid "v - Cost, replacement price" (for example, 1,00). 3. Click "Save changes" and the form will submit but with no price shown in the table on the next page. 4. Apply the patch, update the CSS (yarn build) and restart everything (restart_all). 5. Repeat steps 1-3. A red error message will now appear to the right of the input field, notifying the user that only numbers and periods are allowed. The user will not be able to submit the form. 6. The same change was also made to the batch modification form under Cataloging > Batch item modification. --- Koha/UI/Form/Builder/Item.pm | 7 +++ .../prog/css/src/staff-global.scss | 20 -------- .../prog/en/includes/html_helpers.inc | 5 +- .../prog/en/modules/cataloguing/additem.tt | 2 +- .../prog/en/modules/tools/batchMod-edit.tt | 2 +- .../prog/js/cataloging_additem.js | 19 -------- .../intranet-tmpl/prog/js/pages/batchMod.js | 19 -------- t/db_dependent/Koha/UI/Form/Builder/Item.t | 46 ++++++++++++++++++- 8 files changed, 56 insertions(+), 64 deletions(-) diff --git a/Koha/UI/Form/Builder/Item.pm b/Koha/UI/Form/Builder/Item.pm index cbc52522c3..c05fadeebd 100644 --- a/Koha/UI/Form/Builder/Item.pm +++ b/Koha/UI/Form/Builder/Item.pm @@ -403,6 +403,13 @@ sub generate_subfield_form { value => $value, }; } + elsif ( $subfield->{kohafield} =~ /items\.(replacementprice)/ ) { + $subfield_data{marc_value} = { + type => 'replacementprice', + id => $subfield_data{id}, + value => $value + }; + } else { # it's a standard field $subfield_data{marc_value} = { diff --git a/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss b/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss index f29e167ab0..37839fcbcf 100644 --- a/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss +++ b/koha-tmpl/intranet-tmpl/prog/css/src/staff-global.scss @@ -4895,24 +4895,4 @@ div .suggestion_note { display: block; } } -} - -#cat_additem .replacementprice_error_message { - color: red; - margin-left: 20vw; -} - -#tools_batchMod-edit .replacementprice_error_message { - color: red; - margin-left: 24.5vw; -} - -.replacementprice_error_message.hidden { - display: none; -} - -@media (max-width: 1000px) { - .replacementprice_error_message { - margin-left: 23.5vw; - } } \ No newline at end of file diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc index fa0382b8cf..9dc5521d04 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc @@ -170,6 +170,8 @@ [% ELSIF ( mv.type == 'datetime_field' ) %] + [% ELSIF ( mv.type == 'replacementprice' ) %] + [% END %] [% IF subfield.kohafield == 'items.more_subfields_xml' %] @@ -217,9 +219,6 @@ - [% IF subfield.subfield == 'v' %] - - [% END %] [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt index cec36d91e6..ab679acc71 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt @@ -200,7 +200,7 @@
-
+ [% INCLUDE 'csrf-token.inc' %] [% IF (popup) %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt index b2b3161f21..66133df5e1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt @@ -156,7 +156,7 @@ [% END # /IF notfounditemnumbers.size %] - + [% INCLUDE 'csrf-token.inc' %] diff --git a/koha-tmpl/intranet-tmpl/prog/js/cataloging_additem.js b/koha-tmpl/intranet-tmpl/prog/js/cataloging_additem.js index 3ef01ddcc5..d4f28bbf84 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/cataloging_additem.js +++ b/koha-tmpl/intranet-tmpl/prog/js/cataloging_additem.js @@ -4,25 +4,6 @@ var browser = KOHA.browser(searchid, parseInt(biblionumber, 10)); browser.show(); $(document).ready(function(){ - $("#cataloguing_additem_newitem form").on("submit", function (e) { - var replacementprice = $("input[name='items.replacementprice']").val(); - if (!/^[0-9.]+$/.test(replacementprice)) { - e.preventDefault(); - return false; - } - return true; - }); - - $("input[name='items.replacementprice']").on("input", function () { - var value = $(this).val(); - var isValid = /^[0-9.]*$/.test(value); - - if (!isValid) { - $('.replacementprice_error_message').removeClass('hidden'); - } else { - $('.replacementprice_error_message').addClass('hidden'); - } - }); // Remove the onclick event defined in browser.js, // otherwise the deletion confirmation will not work correctly diff --git a/koha-tmpl/intranet-tmpl/prog/js/pages/batchMod.js b/koha-tmpl/intranet-tmpl/prog/js/pages/batchMod.js index 84e86277d9..78c16aaae2 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/pages/batchMod.js +++ b/koha-tmpl/intranet-tmpl/prog/js/pages/batchMod.js @@ -118,25 +118,6 @@ function hideAllColumns() { } $(document).ready(function () { - $('form[action="batchMod.pl"]').on("submit", function (e) { - var replacementprice = $("input[name='items.replacementprice']").val(); - if (!/^[0-9.]+$/.test(replacementprice)) { - e.preventDefault(); - return false; - } - return true; - }); - - $("input[name='items.replacementprice']").on("input", function () { - var value = $(this).val(); - var isValid = /^[0-9.]*$/.test(value); - - if (!isValid) { - $('.replacementprice_error_message').removeClass('hidden'); - } else { - $('.replacementprice_error_message').addClass('hidden'); - } - }); var items_table = KohaTable("itemst", { "columnDefs": [ diff --git a/t/db_dependent/Koha/UI/Form/Builder/Item.t b/t/db_dependent/Koha/UI/Form/Builder/Item.t index 251287a530..d63813f7af 100755 --- a/t/db_dependent/Koha/UI/Form/Builder/Item.t +++ b/t/db_dependent/Koha/UI/Form/Builder/Item.t @@ -16,7 +16,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 10; +use Test::More tests => 11; use Test::MockModule; use Data::Dumper qw( Dumper ); use utf8; @@ -456,3 +456,47 @@ sub setup_mss { } )->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } ); } + +subtest 'replacementprice handling' => sub { + plan tests => 3; + + my $subfield = { + kohafield => 'items.replacementprice', + id => 'tag_952_subfield_v_12345', + value => '123.45', + }; + + my $expected_marc_value = { + type => 'replacementprice', + id => 'tag_952_subfield_v_12345', + value => '123.45', + }; + + my $item = Koha::Item->new(); + + my %subfield_data; + if ( $subfield->{kohafield} =~ /items\.(replacementprice)/ ) { + $subfield_data{marc_value} = { + type => 'replacementprice', + id => $subfield->{id}, + value => $subfield->{value}, + }; + } + + ok( + exists $subfield_data{marc_value}, + "Subfield data contains 'marc_value' key" + ); + + is( + $subfield_data{marc_value}->{type}, + $expected_marc_value->{type}, + "Subfield 'type' is correctly set to 'replacementprice'" + ); + + is_deeply( + $subfield_data{marc_value}, + $expected_marc_value, + 'Subfield data is processed correctly for replacementprice' + ); +}; -- 2.39.5