[Koha-patches] [PATCH] Bug 10237 - Enforce authorized value character limitations

Owen Leonard oleonard at myacpl.org
Mon May 13 18:36:16 CEST 2013


An authorized value should not have spaces or special characters other
than underscores and hyphens in it, but this limitation is not enforced.
This patch adds client-side validation blocking entries which contain
anything other than numbers, letters, underscores, or dashes. Changes
include:

- The addition of a custom alphanumeric validation scheme which can be
  used anywhere in Koha.
- The addition of a new translatable warning message which appears when
  the alphanumeric validation scheme is violated.
- The addition of rules to the authorized_values.tt template to enable
  the new alphanumeric validation scheme.

To test, apply the patch and clear your browser cache.

With an existing category of authorized values (like ccode):

- Click to add new authorized value
- Try to enter invalid data in the "authorized value" form field. You
  should see a warning.
- Try to submit a form with the invalid data. You should be prevented
  from doing to.
- Correct the invalid data to conform to the validation rules. Warnings
  should disappear.
- Submit the form. It should submit correctly.

With a new category:

- Click to create a new category.
- Try to enter invalid data in the "category" and "authorized value"
  form fields. You should see warnings.
- Try to submit a form with the invalid data. You should be prevented
  from doing to.
- Correct the invalid data to conform to the validation rules. Warnings
  should disappear.
- Submit the form. It should submit correctly.
---
 .../prog/en/includes/validator-strings.inc         |    1 +
 koha-tmpl/intranet-tmpl/prog/en/js/staff-global.js |    4 +++
 .../prog/en/modules/admin/authorised_values.tt     |   26 +++++++++++++++-----
 3 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/validator-strings.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/validator-strings.inc
index 2ca4430..5259ba7 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/validator-strings.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/validator-strings.inc
@@ -11,6 +11,7 @@ $(document).ready(function(){
         number: _("Please enter a valid number."),
         digits: _("Please enter only digits."),
         equalTo: _("Please enter the same value again."),
+        alphanumeric: _("Please use only letters, numbers, underscores, or dashes."),
         maxlength: $.validator.format(_("Please enter no more than {0} characters.")),
         minlength: $.validator.format(_("Please enter at least {0} characters.")),
         rangelength: $.validator.format(_("Please enter a value between {0} and {1} characters long.")),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/staff-global.js b/koha-tmpl/intranet-tmpl/prog/en/js/staff-global.js
index 2bf385f..25c6d59 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/js/staff-global.js
+++ b/koha-tmpl/intranet-tmpl/prog/en/js/staff-global.js
@@ -53,3 +53,7 @@ function openWindow(link,name,width,height) {
     height = (typeof height == "undefined")?'400':height;
     var newin=window.open(link,name,'width='+width+',height='+height+',resizable=yes,toolbar=false,scrollbars=yes,top');
 }
+
+jQuery.validator.addMethod("alphanumeric", function(value, element) {
+    return this.optional(element) || /^[a-z0-9_\-]+$/i.test(value);
+});
\ No newline at end of file
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt
index 5ba3072..1bcf228 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt
@@ -27,6 +27,20 @@
         $("#branches option:first").attr("selected", "selected");
     }
     $('#icons').tabs();
+
+    $("#authval_form").validate({
+        rules: {
+            "category": {
+                required: true,
+                alphanumeric: true
+            },
+            "authorised_value": {
+                required: true,
+                alphanumeric: true
+            }
+        }
+    });
+
 });
 //]]>
 </script>
@@ -75,24 +89,24 @@ $(document).ready(function() {
 
     [% IF ( action_modify ) %]<div class="note"><strong>NOTE:</strong> If you change an authorized value, existing records using it won't be updated.</div>[% END %]
 
-	<form action="[% script_name %]" name="Aform" method="post">
+    <form action="[% script_name %]" name="Aform" method="post" id="authval_form">
 	<input type="hidden" name="op" value="add_validate" />
     <input type="hidden" name="offset" value="[% offset %]" />
         <fieldset class="rows"><ol>
         <li>
-        [% IF ( action_add_category ) %]<label for="category">Category: </label>
-            <input type="text" name="category"  id="category" size="10" maxlength="10" class="focus" />
+        [% IF ( action_add_category ) %]<label for="category" class="required">Category: </label>
+            <input type="text" name="category"  id="category" size="10" maxlength="10" class="focus" required="required" />
 			 [% ELSE %]<span class="label">Category</span>
 		<input type="hidden" name="category" value="[% category %]" />	 [% category %]
 			 [% END %]
         </li>
         <li>
-            <label for="authorised_value">Authorized value: </label>
+            <label for="authorised_value" class="required">Authorized value: </label>
      [% IF ( action_modify ) %]<input type="hidden" id="id" name="id" value="[% id %]" />[% END %]
             [% IF ( action_add_category ) %]
-            <input type="text" id="authorised_value" name="authorised_value" value="[% authorised_value %]" maxlength="80" />
+            <input type="text" id="authorised_value" name="authorised_value" value="[% authorised_value %]" maxlength="80" required="required"/>
             [% ELSE %]
-            <input type="text" id="authorised_value" name="authorised_value" value="[% authorised_value %]" maxlength="80" class="focus" />
+            <input type="text" id="authorised_value" name="authorised_value" value="[% authorised_value %]" maxlength="80" class="focus" required="required"/>
             [% END %]
         </li>
         <li>
-- 
1.7.9.5


More information about the Koha-patches mailing list