Bug 10848

Summary: Allow configuration of mandatory/required fields on the suggestion form in OPAC
Product: Koha Reporter: Katrin Fischer <katrin.fischer>
Component: OPACAssignee: Jacek Ablewicz <abl>
Status: CLOSED FIXED QA Contact: Testopia <testopia>
Severity: enhancement    
Priority: P5 - low CC: abl, aleisha, jonathan.druart, kyle, lisettepalouse+koha, marjorie.barry-vila, nick, patrick.robitaille, sandboxes, tomascohen, valerie.bertrand, veron
Version: Main   
Hardware: All   
OS: All   
See Also: http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=9846
Change sponsored?: --- Patch complexity: Small patch
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:
Attachments: Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #1/2
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #2/2
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #2/2
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #1/2
Bug 10848 [QA Followup] - Allow configuration of mandatory/required fields on the suggestion form in OPAC #2/2
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #2/2
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #1/2
Bug 10848 [QA Followup] - Allow configuration of mandatory/required fields on the suggestion form in OPAC #2/2
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #2/2
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #1/2
Bug 10848 [QA Followup] - Allow configuration of mandatory/required fields on the suggestion form in OPAC #2/2
[PASSED QA] Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #1/2
[PASSED QA] Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #2/2
[PASSED QA] Bug 10848 [QA Followup] - Allow configuration of mandatory/required fields on the suggestion form in OPAC #2/2
Bug 10848 [QA Followup] - Title should behave the same as other required fields
Bug 10848: (QA followup) use the 'multiple' syspref type instead
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC - part 2
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC - part 2
Bug 10848 - [QA Followup] Add missing branch option and fix pattern for copyrightdate
[PASSED QA] Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC
[PASSED QA] Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC - part 2
[PASSED QA] Bug 10848 - [QA Followup] Add missing branch option and fix pattern for copyrightdate

Description Katrin Fischer 2013-09-09 15:37:04 UTC
Several libraries have been suggesting that ISBN should also be mandatory on the suggestion form in OPAC. As some libraries might disagree but prefer other fields it would be nice to have a configuration option for mandatory/required fields.
Comment 1 Jacek Ablewicz 2014-06-08 10:58:56 UTC Comment hidden (obsolete)
Comment 2 Jacek Ablewicz 2014-06-08 10:59:07 UTC Comment hidden (obsolete)
Comment 3 Nick Clemens (kidclamp) 2014-06-12 00:34:58 UTC Comment hidden (obsolete)
Comment 4 Nick Clemens (kidclamp) 2014-06-12 00:37:10 UTC
First time using git-bz, both patches applied and tested and signing off on both
Comment 5 Jonathan Druart 2014-06-13 14:01:34 UTC
Comment on attachment 28721 [details] [review]
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #1/2

Review of attachment 28721 [details] [review]:
-----------------------------------------------------------------

::: koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-suggestions.tt
@@ +39,5 @@
>                              <form action="/cgi-bin/koha/opac-suggestions.pl" method="post">
>                                  <fieldset class="rows">
>                                      <ol>
>                                          <li><label class="required" for="title">Title:</label><input type="text" id="title" name="title"  maxlength="255" /></li>
> +                                        <li><label [% IF (mandatoryfields.author) %]class="required" [% END %]for="author">Author:</label><input type="text" id="author" name="author"  maxlength="80" /></li>

It is not allowed to add template toolkit directives into html tags (see http://wiki.koha-community.org/wiki/Coding_Guidelines#HTML1:_Template_Toolkit_markup_inside_HTML).

@@ +319,5 @@
>              _alertString += _("- You must enter a Title") + "\n";
>          }
> +        [% IF (mandatoryfields.author) %]if (f.author.value.length == 0){
> +            _alertString += _("- You must enter an Author") + "\n";
> +        }[% END %]

Maybe it could be better to use the required="required" html attribute on the input element, don't you think?
Comment 6 Jonathan Druart 2014-06-13 14:02:15 UTC
Comment on attachment 28792 [details] [review]
Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC #2/2

Review of attachment 28792 [details] [review]:
-----------------------------------------------------------------

::: koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
@@ +534,5 @@
> +              class: multi
> +            - "<br />Field names you can set here as mandatory for OPAC suggestions include: <code>author</code>, <code>copyrightdate</code>,"
> +            - "<code>isbn</code>, <code>publishercode</code>, <code>place</code>, <code>itemtype</code> and <code>patronreason</code>."
> +            - "Note: <code>title</code> field would be allways implicitly required, no need to enter it here."
> +        -

typo "allways"
Comment 7 Jacek Ablewicz 2014-06-20 11:32:21 UTC Comment hidden (obsolete)
Comment 8 Jacek Ablewicz 2014-06-20 11:33:39 UTC Comment hidden (obsolete)
Comment 9 Jacek Ablewicz 2014-06-20 12:06:45 UTC
(In reply to Jonathan Druart from comment #5)
 
> Maybe it could be better to use the required="required" html attribute on
> the input element, don't you think?

Looks like a good idea, especially after I learned what it is and how it works ;). I've rewritten this patch to make use of this new attribute, I agree it does work a lot nicer that way, at least in modern browsers.
I left Check() function still remaining as fallback mechanism for older/legacy browsers (not sure it's a best possible approach ?), also I think it may still come in handy e.g. in case when including some additional / more specific validity checks on the form contents may be desired in the future.
Comment 10 Jacek Ablewicz 2014-06-20 12:32:39 UTC
On (not completely) unrelated subject: I think it would be quite beneficial to have some general-purpose utility function for: fetching + spliting + checking & (optionally) de-duplicating system preferences which are in 'value1|value2|...' form. Unless there already is such a function available somewhere, which I'm not aware of?
Comment 11 Jonathan Druart 2014-06-20 12:54:13 UTC
(In reply to Jacek Ablewicz from comment #10)
> On (not completely) unrelated subject: I think it would be quite beneficial
> to have some general-purpose utility function for: fetching + spliting +
> checking & (optionally) de-duplicating system preferences which are in
> 'value1|value2|...' form. Unless there already is such a function available
> somewhere, which I'm not aware of?

Jacek,
Thanks for your new patch!
No this kind of function does not exist. But some syspref use '|', and others ',', ';'.
So I am not sure that could be useful.
Comment 12 Jonathan Druart 2014-12-11 13:24:27 UTC Comment hidden (obsolete)
Comment 13 Jonathan Druart 2014-12-11 13:24:32 UTC Comment hidden (obsolete)
Comment 14 Jonathan Druart 2014-12-11 13:24:36 UTC Comment hidden (obsolete)
Comment 15 Jonathan Druart 2014-12-11 13:24:52 UTC
patch rebased, small conflict on updatedb.pl
Comment 16 Biblibre Sandboxes 2014-12-11 14:48:44 UTC
Patch tested with a sandbox, by Valérie Bertrand <valerie.bertrand@univ-lyon3.fr>
Comment 17 Biblibre Sandboxes 2014-12-11 14:49:28 UTC Comment hidden (obsolete)
Comment 18 Biblibre Sandboxes 2014-12-11 14:49:32 UTC Comment hidden (obsolete)
Comment 19 Biblibre Sandboxes 2014-12-11 14:49:35 UTC Comment hidden (obsolete)
Comment 20 Bertrand 2014-12-11 15:04:08 UTC
I followed the test plan, and changed the fields configuration as asked, both in IE 11 and Firefox 33.1. It works well. unfortunately I was not able ti test the patch on older browsers.
Comment 21 Kyle M Hall (khall) 2014-12-19 14:15:33 UTC Comment hidden (obsolete)
Comment 22 Kyle M Hall (khall) 2014-12-19 14:15:42 UTC Comment hidden (obsolete)
Comment 23 Kyle M Hall (khall) 2014-12-19 14:15:45 UTC Comment hidden (obsolete)
Comment 24 Kyle M Hall (khall) 2014-12-19 14:15:47 UTC Comment hidden (obsolete)
Comment 25 Tomás Cohen Arazi (tcohen) 2015-02-09 20:16:42 UTC
I like the feature, but failing because of the fact that being the available options a fixed list, I think it fits better with the 'multiple' syspref type, which is less error-prone, and user-friendly.

If anyone objects failing it, please mark as 'In discussion' and add your comments.
Comment 26 Tomás Cohen Arazi (tcohen) 2015-02-11 13:10:31 UTC Comment hidden (obsolete)
Comment 27 Tomás Cohen Arazi (tcohen) 2015-02-11 13:13:37 UTC
I put it back on the QA queue. Hope someone can take a look ASAP.
Comment 28 Jonathan Druart 2015-02-12 10:52:25 UTC
The description of the pref is " (Note: title field would be always implicitly required, no need to enter it here).".

But it's wrong, if the pref is empty, the title is mandatory. You can save and get "Your suggestion has been submitted.", but nothing has been saved.

Marked as Failed QA.
Comment 29 Katrin Fischer 2015-04-05 23:04:57 UTC
*** Bug 9846 has been marked as a duplicate of this bug. ***
Comment 30 Katrin Fischer 2016-07-21 16:12:27 UTC
Could this be revived?
Comment 31 Jacek Ablewicz 2016-08-05 09:09:00 UTC Comment hidden (obsolete)
Comment 32 Jacek Ablewicz 2016-08-05 09:10:34 UTC Comment hidden (obsolete)
Comment 33 Jacek Ablewicz 2016-08-05 09:11:28 UTC Comment hidden (obsolete)
Comment 34 Jacek Ablewicz 2016-08-05 09:11:44 UTC Comment hidden (obsolete)
Comment 35 Jacek Ablewicz 2016-08-05 09:13:36 UTC Comment hidden (obsolete)
Comment 36 Jacek Ablewicz 2016-08-05 09:13:47 UTC Comment hidden (obsolete)
Comment 37 Jacek Ablewicz 2016-08-05 09:27:58 UTC
In this revised version I dropped the old form validation handler entirely, in favour of the "required" attributes as a sole method of enforcement. Not sure if the special treatment for old / legacy / non-compliant browsers is still needed? 

If deemed necessary, I may try to re-add such an event handler which will be more on par with the current coding guidelines.
Comment 38 Owen Leonard 2016-08-05 13:19:29 UTC
I find that it's not possible to make the "library" selection mandatory. Is there a reason for that?

Note that there is a JS validation plugin available in the OPAC if you want to use it. See opac-memberentry.tt for an example. The plugin can be set up to validate a form based on the markup (requiring fields which are , so it's not necessary to dynamically configure the fields in the JS if you don't want to.
Comment 39 Barbara Johnson 2016-08-06 22:45:16 UTC Comment hidden (obsolete)
Comment 40 Barbara Johnson 2016-08-06 22:45:30 UTC Comment hidden (obsolete)
Comment 41 Katrin Fischer 2016-08-07 13:34:34 UTC
This seems to work well, but I'd like to address Owen's comment#38

The visibility of the library selection depends on AllowPurchaseSuggestionBranchChoice. It could be included in the mandatory list, maybe with a note about the dependency on the other pref in the text?

I think it would be nice if the error messages were a bit more standard. If you are missing multiple fields, only the first will be highlighted. If I enter a wrong format into the year field, it says 'Please use the specified format' (translated from German), but gives no clue about the format. 
Would this be improved by Owen's suggestion of using the existing JS validation plugin?

Jacek, could you please take a look?
Comment 42 Jacek Ablewicz 2016-08-08 10:26:11 UTC
(In reply to Owen Leonard from comment #38)
> I find that it's not possible to make the "library" selection mandatory. Is
> there a reason for that?

No, I just forgot about it. While it would not affect form submission itself (there will be always some non-empty value submitted in case when AllowPurchaseSuggestionBranchChoice is enabled), similar logic applies for itemtype, and yet our librarians found it vitally important that this field too could be marked as mandatory. And I agreed, good for consistency. Also still useful as an optional remainder for the user that a given field is relatively important and that they should pay a closer attention to set it accordingly.

> Note that there is a JS validation plugin available in the OPAC if you want
> to use it. See opac-memberentry.tt for an example. The plugin can be set up
> to validate a form based on the markup (requiring fields which are , so it's
> not necessary to dynamically configure the fields in the JS if you don't
> want to.

Hm, would it be as simple as adding jquery.validate.min.js include and one jquery call

   $('#add_suggestion_form').validate();

to the template? Let me see how it works ( please do not adjust your set)..
Comment 43 Jacek Ablewicz 2016-08-08 12:33:58 UTC
(In reply to Katrin Fischer from comment #41)

> Would this be improved by Owen's suggestion of using the existing JS
> validation plugin?

JQuery validation plugin (instead of - or as an addition to - HTML5 attributes alone) seems to be a better choice (more configurable, extensible, and probably more predictable with old browsers as well). However so far I have zero experience with this plugin (or any other jQuery plugins for that matter).
Comment 44 Jacek Ablewicz 2016-08-08 16:52:53 UTC
> > Note that there is a JS validation plugin available in the OPAC if you want
> > to use it. See opac-memberentry.tt for an example. The plugin can be set up
> > to validate a form based on the markup (requiring fields which are , so it's
> > not necessary to dynamically configure the fields in the JS if you don't
> > want to.
> 
> Hm, would it be as simple as adding jquery.validate.min.js include and one
> jquery call
> 
>    $('#add_suggestion_form').validate();
> 
> to the template? Let me see how it works ( please do not adjust your set)..

So far so good, it's working (almost) fine, apart from two small issues:

1) for 'pattern' rule to work I guess additional include (additional-methods.js or pattern.js ?) may be needed, but neither seems to be anywhere in the code tree yet (?) - or maybe there is some other bug report waiting in the queue which includes one of them, that I can test and sign instead?
2) translateability - no matter what I try to add to the .po files etc., "This field is required" still appears only in english (but so is the "Please enter a valid email address." message in opac-memberentry.tt ?). Perhaps it's just my test install which is missing something, or in order for this message to be translateable some additional trickery may be needed..
Comment 45 Katrin Fischer 2016-08-08 20:43:52 UTC
Hi Jacek, how do you add to the po files?
You should not need to change anything manually there. For a git installation try:
- switch to misc/translator in your koha directory
- perl translate update <langcode>
- perl translate install <langcode>

The first updates the templates the second installs them from the po file for testing.
Comment 46 Jacek Ablewicz 2016-08-09 05:20:14 UTC
(In reply to Katrin Fischer from comment #45)
> Hi Jacek, how do you add to the po files?
> You should not need to change anything manually there. For a git
> installation try:
> - switch to misc/translator in your koha directory
> - perl translate update <langcode>
> - perl translate install <langcode>
> 
> The first updates the templates the second installs them from the po file
> for testing.

That's how I've been trying to test it. Well, manually adding a few odd extra strings in the .po file and 'translate install' without 'translate update' is not below my dignity too ;) - I know it's not a proper way, but it should work just fine for testing purposes (usually does in similar circumstances).

Problem is that messages embedded in the .js file ("This field is required", "Please enter a valid email address", ...) are not picked up for translation. This is most likely normal, I just wasn't aware of that.

Looks like there are several ways to fix this issue, either globally for OPAC/bootstrap - like it was done for intranet/prog in Bug 6209, or locally 
in the given OPAC templates, e.g. using a combination of 

    jQuery.extend(jQuery.validator.messages, {
        required: _("This field is required."),
        email: _("Please enter a valid email address."),
        ...

for the "regular" mandatory fields, and/or

    jQuery.validator.addMethod("year", function(value, element) {
        ...
    }, _("Please enter a year in the valid format (YYYY, YY--)"));


for a field with the pattern. I guess preferably it should be fixed globally for the OPAC (opac-memberentry.tt is affected by this too), but I'm a bit out of my depth here - I'll better open a separate bug report for that part.
Comment 47 Owen Leonard 2016-08-09 12:15:20 UTC
(In reply to Jacek Ablewicz from comment #46)
> I'll better open a separate bug report for that part.

I would be fine with having this form depend on browser-based validation using the "required" attribute for now, and to consider another layer of JS validation as a separate bug.
Comment 48 Jacek Ablewicz 2016-08-12 10:25:31 UTC Comment hidden (obsolete)
Comment 49 Jacek Ablewicz 2016-08-12 10:37:37 UTC
(In reply to Katrin Fischer from comment #41)

> If I enter a wrong format into the year field, it says 'Please use the specified
> format' (translated from German), but gives no clue about the format. 

On the 2nd thought, previous pattern ("[12][0-9\-\?]{3}") wasn't making much sense - copyrightdate in the DB is just smallint(5).

Added a hint for the user regarding the required format for that field as a title="", i.e. tooltip. Doesn't always work quite perfectly (depending on the browser), but hopefully it would be good enough for the time being / close enough for government work.
Comment 50 Katrin Fischer 2016-08-14 10:32:43 UTC
The follow-up is small - I think it's safe to treat it as a QA follow-up.
Comment 51 Katrin Fischer 2016-08-14 12:45:44 UTC
Looking at it again... having library mandatory appears a bit silly as it's currently not possible to 'unset' it - so I got not idea on how to trigger this :)
Comment 52 Katrin Fischer 2016-08-14 12:55:07 UTC
Maybe something for a different bug: What about renaming copyright date to publication year? I think this could make the format more obvious too. Also... is collection title meant to be series? We have this showing up in some places in Koha and it seems related to UNIMARC - but not sure what it is supposed to be in MARC21.
Comment 53 Katrin Fischer 2016-08-14 12:57:54 UTC
There are some things not super-perfect here, but I think no blockers. 
Making the library mandatory doesn't hurt - we can think about having it empty or to 'all libraries' separately.
The new format check keeps you from submitting something that cannot be stored in the database - improving the GUI is also something we can do separately. Maybe we could just display a hint behind the field in a grey font as we do in other places.

Passing QA.
Comment 54 Katrin Fischer 2016-08-14 12:58:58 UTC
Created attachment 54430 [details] [review]
[PASSED QA] Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC

This patch adds a configuration option which allows to define which
fields should be mandatory for a patron purchase suggestion form in OPAC.

Test plan:

1/ Apply patch.
2/ Play with the new OPACSuggestionMandatoryFields system preference
(select some fields as manadatory, select all, deselect all, try to
submit some suggestions with mandatory fields filled and/or not
filled etc.) to ensure that required fields (and only required fields)
are enforced in the browser to be filled.
3/ With all options deselected, 'Title' field should still be
mandatory (by default).

Signed-off-by: barbara johnson <barbara.johnson@bedfordtx.gov>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Comment 55 Katrin Fischer 2016-08-14 12:59:46 UTC
Created attachment 54431 [details] [review]
[PASSED QA] Bug 10848 - Allow configuration of mandatory/required fields on the suggestion form in OPAC - part 2

Add 'OPACSuggestionMandatoryFields' syspref definition plus
an atomic DB update for the new preference.

Signed-off-by: barbara johnson <barbara.johnson@bedfordtx.gov>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Comment 56 Katrin Fischer 2016-08-14 13:01:01 UTC
Created attachment 54432 [details] [review]
[PASSED QA] Bug 10848 - [QA Followup] Add missing branch option and fix pattern for copyrightdate

- HTML5 input pattern for copyrightdate was not taking into an account
the database field type, which is smallint(5)
- added title="..." for the year field so the acceptable date format
should be displayed as a hint/tooltip in the browser
- added an missing option for configuring branch/library as mandatory

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Comment 57 Kyle M Hall (khall) 2016-09-02 17:00:28 UTC
Pushed to master for 16.11, thanks Jacek!
Comment 58 Owen Leonard 2016-09-09 17:33:34 UTC
*** Bug 17166 has been marked as a duplicate of this bug. ***