How to validate all checkboxes are checked?

Systems Department January 7, 2013

We have large number of transitions in a workflow and each transition pops up a screen prompting the user to check the box next to a set of conditions. These are things like "Has the Manager Approved", "Has the CFO Approved" etc. I use a multicheckbox for these.

I would like to stop a user form transitioning if all the checkboxes are not checked. I am using script runner and am pretty sure I could add a large number of cfValues['Field'][0] == "Has the Manager Approved", etc etc.. However this is very cumbersome and a nightmare if any of the fields are changed. Is there a simpler Validation function I can use that would work? Something like.. Field.AllValues are 'Yes'. I have Script Runner and Behaviours installed.

Thank you :)

3 answers

1 vote
Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2013

Instead of multiple single checkboxes use muli checkbox custom field. So you can check all checks in a collection.

If not applicable fetch all CF check boxes and compare their names to a mask/template and only validate those that match. Be sure to name CF's properly

def customFieldManager = componentManager.getCustomFieldManager()
def customFields = customFieldManager.getCustomFieldObjects(issue)
customFields.each {cf ->
   if (cf.name =~ 'Approved by' ) {
     //do your stuff here
   }
}

Systems Department January 8, 2013

This seems like a really good approach.

I am assuming I would put a version of this script in the Script-Runner Validator for the transition. If all my custom fields have a similar name I would look for that name so the script would be reusable on multiple transitions without having to re-write it.

And... that's where my knowledge stops.

I know that the "//do your stuff here", I need to ensure all the checks are checked, and, that if they are, I need to return true (I think). Otherwise I return false and the step fails validation.

Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 9, 2013

I just looked at the issue again. I was surprised there is no single checkbox custom field type.

This means you either have one multi checkbox field for each approval checkbox or ONE multi checkbox field with options for each approval.

Let me know what is the case and I'll look for a solution.

I suggest you learn some Jira API using this page https://developer.atlassian.com/static/javadoc/jira/5.0/reference/com/atlassian/jira/issue/fields/CustomField.html

Systems Department January 9, 2013

I presently have 15 MultiCheck box fields each with a number of options that all need to be checked. The field names are all predictable and named similarily, so a regex to find the field would work. The pseudocode would be something like this:

for each customfield that contains "Check_" do {

If all options in customfield are not null {

return true

} else {

return false // Do not validate

}

}

Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 10, 2013

You can use .getValue(issue) method to get a collection of selected options

i did not have time to learn API to get all options for a multiselect filed to compare available options to selected. It might be easier to add all options from all relevant custom fields to one collection then compare it to desired set/collection of options. I might be able to provide a snippet later

Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 10, 2013
def customFieldManager = componentManager.getCustomFieldManager()
def customFields = customFieldManager.getCustomFieldObjects(issue)

def values = []
customFields.each {cf ->
   if (cf.getName() =~ "Check_" && cf.getCustomFieldType().getName() == "Multi Checkboxes") {
     values += cf.getValue(issue)*.toString()
}
def validChecks = ['CFO', 'CTO'] //add all your required options here
return values.containsAll(validChecks) ? true : false

Like Pavel Strongin likes this
1 vote
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2013

you can achive by using javascript as follows

<script type="text/javascript">
var selected = new Array();
$(document).ready(function() {
  $("input:checkbox[name=customfieldID]:checked").each(function() {
       selected.push($(this).val());
  });
  if(selected.length <5){
	alert("enter your message here..");
	return false;
  }

});

</script>

you have to do little bit modifications as per your requirement and your jira version.

0 votes
Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 7, 2013
Do we need to really do this a script validation? If each of these is made as Custom Field with just one option (for example 'Has The Manager Approved') and add the validator from JIRA Suite Utilities - https://confluence.atlassian.com/display/JIRA/Using+Validators+to+Make+Custom+Fields+Required+on+Transition+Screens
Systems Department January 7, 2013

I have tried to use the following line in the simple validator box:

Collections.frequency(Arrays.asList(cfValues['CustomField']),null) == 0

But it does not quite work as I hoped.

Systems Department January 7, 2013

I could make a custom field for each checkbox. However that would be an insane number of custom fields. With the additional workload of if I want to add an element to the list I need to edit a screen and add the field. This is something I am willing to do if there is no elegant validator solution.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 7, 2013

I think, not simpler but more elegant and maintenance-free, is to get the list of option values for that custom field and make sure they're all present. I would write a condition in groovy, rather than using the "simple scripted condition", if that's what you're using. Don't have time to write it for you, if I get a chance, I'll post.

Suggest an answer

Log in or Sign up to answer