Scene 1: Agile advocate in the room is screaming! How can you have 2 fix versions for a single ticket? How? How? How? Several scenes (and days) later, Scene N: Q: We need to restrict the fixVersion(s) field to have only one version. And we need it before the next meeting. Can you? A: Yup, you are not the first one to ask. Let's do it! Well, that was the plot! The fixVersion(s) field in JIRA allows multiple versions and this was one of the cases were the customer wanted to restrict the field to have only one version selected. Doing this was pretty easy in the earlier versions of JIRA where it was a simple multiselect field. All you had to do was to find the appropriate velocity template that is used to render the field and make it a single select instead of multi select. The template was verions-edit.vm under /atlassian-jira/WEB-INF/classes/templates/jira/issue/field and that was it! In JIRA 4.4.3, things are a bit more tricky. The fixVersion field is rendered using the same template but there is no multi-select field on the screen now. Well, there is one but it is hidden! Instead, the visible field is an Ajax field that populates the options as the user starts typing matching letters. And as the user selects one of the options, the multi select field takes that value. You can select more value, ofcourse! And that is what we wanted to restrict. The easy choice and perhaps the best (we will see why) is to do a Javascript validation on the submission of the form. Adding Javascript on a field is pretty easy in JIRA. All you need to do is to edit the field's description in the appropriate field configuration and add the Javascript there. This has an added advantage that the Javascript needs to be added only in the field configurations that you want and hence you can limit the restrictions for selected projects, all by simple configuration! That is why I said it is probably the best solution. And Javascript validation can be done easily using AJS:
Here is what I did: <script > var formId = AJS.$("#fixVersions").closest('form').attr('id'); AJS.$('#'+formId).submit(function() { var count = AJS.$("#fixVersions :selected").length; if (count > 1){ alert('You can select only one Fix Version for an issue'); return false; } else { return true; } }); </script> And this is what happened when they selected multiple fix versions: But, ah the buts, someone asked the question I was trying to avoid. Isn't this a reactive solution? Throwing an error when they submit the form, after they select multiple versions? Can we do something proactive? i.e. just prevent people from selecting it? Time to hack the AJS scripts that does the population of options. Now, if you trace back from the class used in aui params in the versions-edit.vm, aui-field-versionspicker, you will find that initVersionPickers.js file uses it to create the selector picker. And the function used is AJS.MultiSelect. The AJS.MultiSelect function is declared in /atlassian-jira/includes/ajs/select/MultiSelect.js file. And that, is the file where I put my hack! There might be different places where you can put the hack in but I found it appropriate to place it along side the duplicate check. There is a method _isItemPresent in the MultiSelect.js file which checks whether the option selected is already present or not! I have added few simple lines alongside it to check whether the list has atleast one version selected or not. And if it has, we are going to just alert the user and treat it just like the duplicate scenario. Here is the modified method: _isItemPresent: function (descriptor) { //For fixVersions, return false if more than one version is selected! var elementId = this.options.element.attr('id'); if (elementId == 'fixVersions' && this.lozengeGroup.items.length > 0){ alert('You can select only one Fix Version for an issue'); return true; } // Normal duplicate check var duplicate = false; var value = descriptor.value(); AJS.$.each(this.lozengeGroup.items, function () { if (this.value === value) { duplicate = true; return false; // bail } }); return duplicate; }, And ofcourse, you should check if the field is fixVersions or not because the MutliSelect function is used by other fields like Affected Versions! And that's it! When you try to select more than one version, user gets the alert then and there. The same alert, but well in advance! Remember, the change has to be done in the minnified version of the javascript, MultiSelect-min.js as well. And yes, once this is done, it is applied system-wide. i.e. for all the projects. If you need this only for selected projects or you want to remove the checking for selected projects, add extra bits to check for projects. I would try to avoid that and use the first solution in that case. PS: Don't forget to do this change for every upgrades of JIRA! PS(2): Like this trick? Have a look at the book to see more! Edit: May 25, 2012 What if you are adopting the first method and you want to clear out all the fixVersions after the alert is shown? The following worked in response to Srinivas's query in the comments: <script > var formId = AJS.$("#fixVersions").closest('form').attr('id'); AJS.$('#'+formId).submit(function() { var count = AJS.$("#fixVersions :selected").length; if (count > 1){ alert('You can select only one Fix Version for an issue'); AJS.$("#fixVersions-multi-select").find('em').click(); return false; } else { return true; } }); </script> Here the line in bold clears the selected options by triggering a 'click' on the small close icon! - Tested only on 4.4.3.
61 Comments
J-Tricks
12/12/2011 07:13:52 am
Thanks Matt.
Reply
It occurred to me that you might be able to use the change() function on the select element to avoid hacking the MultiSelect.js file.
Reply
J-Tricks
12/12/2011 08:04:01 am
Yup, The onChange function can alert the user but it isn't preventing the user from adding it. I did try it intially (though the script was slightly different). Maybe use both the alert and the form validation as you suggested.
Reply
mizan
1/19/2012 09:23:10 pm
Hi JTricks,
Reply
J-Tricks
2/5/2012 02:42:34 pm
Mizan,
Reply
Kapil
3/12/2012 01:21:36 am
Hello J-Tricks,
Reply
J-tricks
3/12/2012 02:02:06 am
Glad it worked. Use 'versions' instead of 'fixVersions' to make it work for affected versions.
Reply
SRINIVAS
5/25/2012 08:23:24 am
If you are looking for both fix and affects version, this much easier way.
Reply
J-Tricks
5/25/2012 10:59:57 am
Thanks Srini. But it doesn't work in the later versions as it not using tat template anymore.
SRINIVAS
5/25/2012 12:19:53 pm
atlassian-jira-4.4.5-standalone/atlassian-jira/WEB-INF/classes/templates/jira/issue/field/versions-edit.vm is the exact file because i modified it and i am able to see the changes. Though i changed the class of the select box it is still showing the multiselect options. Don't understand why..
J-Tricks
5/25/2012 12:28:48 pm
Yes, exactly. The multi select option is coming from Javascript elsewhere and not from that velocity template. It is the MultiSelect.js file.
SRINIVAS
5/25/2012 01:08:39 pm
I wrote the onchange function and I am able to see the alert.. Can you tell me how to delete the option that is added?
J-Tricks
5/25/2012 01:48:50 pm
This one will remove all the options selected:
Srinivas
5/25/2012 02:05:28 pm
Thanks Jobin!
J-Tricks
5/25/2012 04:17:34 pm
You won't be able to do that with this approach. For that you will have to adopt the second method mentioned in the tutorial.
SRINIVAS
5/27/2012 09:25:51 am
In some browsers like firefox, the javascript alerts can be controlled by selecting the checkbox (Prevent this page from creating additional dialogs) in the alert. If we select that checkbox, it is not showing any more alerts and there by allowing to select multiple fix versions. Can you please tell me if there is a way to control that?
J-Tricks
5/27/2012 12:06:34 pm
Nope. All JS Hacks are dependent on browsers allowing JS, alerts etc. There is nothing that can be done to override that.
srinivas
5/29/2012 07:46:27 am
The usage of div and span tags creating unnecessary textboxes above the fix version multi select box.
SRINIVAS
5/29/2012 11:12:36 am
Yes i got it now. It is just a manipulation of div tags. I placed this div tag above the existing ones and finally solved this. Please let me know if there any way to clear only one fix version instead of clearing both.
J-Tricks
5/29/2012 11:16:10 am
Cool. Regarding the second approach, see this bit in the post:
Yissar
3/23/2012 12:43:19 am
I am trying to copy the content of the Comments field into my own customfield - Comments1
Reply
Jorge
5/25/2012 07:58:14 am
Hi there. I love your site and writing style!
Reply
J-Tricks
5/25/2012 12:13:20 pm
Try this:
Reply
J-Tricks
6/8/2012 11:06:49 am
lol. Another hack :)
Reply
Royce
6/15/2012 08:53:35 am
I bought the book. Great stuff!
Reply
Royce
6/20/2012 11:27:02 am
My co-worker suggested:
Reply
J-Tricks
6/20/2012 03:17:27 pm
Awesome! Thanks for commenting here. I hope someone will find it useful.
Royce
1/16/2013 10:41:51 am
Well, the .blur() doesn't work in IE 8. :( Someone know a sure way?
Renu
10/17/2012 01:01:04 am
Hi J-Tricks,
Reply
J-Tricks
10/17/2012 03:14:32 pm
Which option did you try?
Reply
Renu
10/17/2012 11:37:55 pm
Hi J-Tricks,
Nancy Belser
10/30/2012 07:42:22 am
My solution to this problem may not please some of you, but for Jira 4.4.x, I replaced the entire versions-edit.vm with an edited 4.1.1 version (only edit being the removal of multi-select). My users do not get the benefit of the new type ahead control, but it was more important to implement single select. I just tested this with Jira 5.1.6 and it still works. I should note that any version-picker custom field would still use the newer multi-select control. Not sure how to deal with that yet.
Reply
Nancy Belser
10/30/2012 03:14:01 pm
Just read Matt's comment about the single select version custom field. Duh!
Reply
J-Tricks
10/30/2012 03:57:33 pm
One problem solved I guess? lol
Reply
nancy.belser
10/31/2012 01:44:53 am
By the way, I love this site and your book! Thanks for the great work!
J-Tricks
10/31/2012 03:01:48 pm
Thanks. No better feeling than knowing someone finds this useful!
Renu
10/31/2012 01:43:54 am
Hi J-Tricks,
Reply
Renu
10/31/2012 07:18:40 pm
Hi Jobin,
Reply
J-Tricks
11/1/2012 01:35:08 am
Oh, that's good to know. I was going to try it out this weekend. Thanks for updating :)
Reply
Srinivas
1/24/2013 09:06:58 am
Hi Renu,
Reply
Renu
11/4/2012 05:28:50 pm
Hi Jobin,
Reply
Mizan
12/6/2012 10:57:06 pm
Hi Jtricks .
Reply
Mizan
1/6/2013 09:32:58 pm
I found a workaround for this . I hide the complete field if the Archive version is present. Like this my fix version cannot contain 2 versions :)
Reply
Srinivas
1/16/2013 09:14:44 am
The below statement seems not working in JIRA 5.2.2.
Reply
Royce
1/16/2013 10:57:36 am
Ha, probably Atlassian changed the UI/libraries again.
Reply
ckr
3/28/2013 12:38:35 am
Hi jtricks!!
Reply
Raj
5/23/2013 05:56:28 am
Jobin
Reply
Tian
6/26/2013 06:26:09 pm
The script seems not working in 5.2, can you check why. Thanks
Reply
Martin
12/3/2013 05:23:56 am
Hi,
Reply
J-Tricks
12/3/2013 10:18:36 am
Did you change in the minnified version of the javascript, MultiSelect-min.js?
Reply
Martin
12/4/2013 05:23:46 am
Yes - I have just double checked. I am using Jira 5.0.6.
Reply
Lanthanide
3/6/2014 03:22:04 pm
Now, can you do something similar for Labels?
Reply
J-Tricks
3/6/2014 03:38:40 pm
Frankly, I wouldn't recommend using labels for this purpose. Sounds more like a Select List field (with pre-defined options) but with the Ajax style of picking the values.
Reply
heshan
10/16/2014 12:36:46 am
Can we disable multi select only for selected projects?
Reply
J-Tricks
10/16/2014 07:35:57 am
This is all javascript. You should be able to add a condition in the script to check for project key.
Reply
Alex Shorkin
9/15/2016 11:02:25 am
What's the current solution of the problem?
Reply
Jira user
4/3/2017 12:08:31 am
Could you please help me with the groovy script which will restrict users to put multiple values in Fix Version field?
Reply
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorJobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. Categories
All
Archives
October 2016
|