From 2621af6377970470e347559d67f0da5de47aaa9f Mon Sep 17 00:00:00 2001 From: Cesar Velez Date: Tue, 23 May 2017 16:36:44 -0400 Subject: [PATCH] LP#1098685: Require OPAC patron holds w/ phone/SMS notification to enter that info This provides some plain vanilla JS form validation, and error feedback using a yellow background color on the culprit input field. Signed-off by: Cesar Velez Signed-off-by: Chris Sharp Signed-off-by: Galen Charlton --- Open-ILS/src/templates/opac/parts/js.tt2 | 31 ++------- Open-ILS/src/templates/opac/parts/place_hold.tt2 | 8 +-- .../web/js/ui/default/opac/holds-validation.js | 76 ++++++++++++++++++++++ 3 files changed, 87 insertions(+), 28 deletions(-) create mode 100644 Open-ILS/web/js/ui/default/opac/holds-validation.js diff --git a/Open-ILS/src/templates/opac/parts/js.tt2 b/Open-ILS/src/templates/opac/parts/js.tt2 index a5c708261c..e581b07ead 100644 --- a/Open-ILS/src/templates/opac/parts/js.tt2 +++ b/Open-ILS/src/templates/opac/parts/js.tt2 @@ -62,6 +62,13 @@ [%- IF ctx.page == 'record' AND ctx.google_books_preview %] [%- END %] + + +[% IF ctx.page == 'place_hold' %] + +[% END %] + [%- IF want_dojo; -%] - -[% IF ctx.page == 'place_hold' %] - -[% END %] - [%- END; # want_dojo -%] diff --git a/Open-ILS/src/templates/opac/parts/place_hold.tt2 b/Open-ILS/src/templates/opac/parts/place_hold.tt2 index a7e7441fd5..c9f1d99240 100644 --- a/Open-ILS/src/templates/opac/parts/place_hold.tt2 +++ b/Open-ILS/src/templates/opac/parts/place_hold.tt2 @@ -45,7 +45,7 @@ function toggleActivationDate() { ELSE; some_holds_allowed = 1; END; END %] -
0 AND enable.radio.parts == 'true' %] onsubmit="return validateHoldForm()" [% END %] > + [% redirect = CGI.param('hold_source_page') || CGI.param('redirect_to') || CGI.referer; @@ -197,7 +197,7 @@ function toggleActivationDate() {

[% l('Notify when hold is ready for pickup?') %]

-
@@ -205,7 +205,7 @@ function toggleActivationDate() { ELSE; l('Email Address:') %] [% ctx.user.email %][% END %]
[%- IF allow_phone_notifications == 'true' %] -
@@ -215,7 +215,7 @@ function toggleActivationDate() {
[%- END -%] [% IF ctx.get_org_setting(ctx.search_ou, 'sms.enable') == 1 %] -
diff --git a/Open-ILS/web/js/ui/default/opac/holds-validation.js b/Open-ILS/web/js/ui/default/opac/holds-validation.js new file mode 100644 index 0000000000..842366c50e --- /dev/null +++ b/Open-ILS/web/js/ui/default/opac/holds-validation.js @@ -0,0 +1,76 @@ +/* JS form validation for holds page alert methods */ +function resetBackgrounds(names){ + for (var key in names) { + if (names.hasOwnProperty(key)) { + var l = document.getElementsByName(names[key]); + if (l.length > 0) { + l[0].style.backgroundColor = ""; + } + } + } +} + +function validateMethodSelections (alertMethodCboxes) { + var needsPhone = false; + var hasPhone = false; + + var needsEmail = false; + var hasEmail = false; + + var needsSms = false; + var hasSms = false; + var inputNames = { e: "email_address", ph: "phone_notify", sms: "sms_notify", carrier: "sms_carrier"}; + resetBackgrounds(inputNames); + + //Array.from(alertMethodCboxes).forEach(function(cbox){ + for (var i = 0; i < alertMethodCboxes.length; i++){ + var cbox = alertMethodCboxes[i]; + if (cbox.checked && !cbox.disabled) { + switch(cbox.id){ + case "email_notify_checkbox": + needsEmail = true; + hasEmail = document.getElementsByName(inputNames.e)[0].innerHTML !== ""; + break; + case "phone_notify_checkbox": + needsPhone = true; + hasPhone = document.getElementsByName(inputNames.ph)[0].value !== ""; + break; + case "sms_notify_checkbox": + needsSms = true; + var smsNumInput = document.getElementsByName(inputNames.sms)[0]; + hasSms = document.getElementsByName(inputNames.carrier)[0].value !== "" && smsNumInput.value !== ""; // todo: properly validate phone nums + break; + } + } + } + + var culprits = []; + var emailOK = (needsEmail && hasEmail) || (!needsEmail); + var phoneOK = needsPhone && hasPhone || (!needsPhone); + var smsOK = needsSms && hasSms || (!needsSms); + + if (!phoneOK) { + culprits.push("phone_notify"); + } + if (!smsOK) { + culprits.push("sms_notify", "sms_carrier"); + } + + var isFormOK = emailOK && phoneOK && smsOK; + return { isValid: isFormOK, culpritNames : culprits }; +} + +function validateHoldForm() { + var res = validateMethodSelections(document.getElementsByClassName("hold-alert-method")); + if (res.isValid) + { + return true; + } else { + alert ("Please complete hold notification method info."); + res.culpritNames.forEach(function(n){ + document.getElementsByName(n)[0].style.backgroundColor = "yellow"; + }); + return false; + } +} + -- 2.11.0