How to validate all checkboxes are checked?

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

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
   }
}

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.

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

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

}

}

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

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

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

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.

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.

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.

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.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

382 views 4 10
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you