Making field conditionally required

Here's my situation. I have Field A and Field B. I have a script that I've written that will display Field B if Field A equals a certain value. What I need to do (and haven't gotten anything to work yet) is make Field B required if it pops up. My javascript is shown below, any help with this is greatly appreciated!

 

<script type="text/javascript">

jQuery(document).ready(function($) {
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
callShowHideFunctions();
});
callShowHideFunctions();

function callShowHideFunctions(){
showHidefields();
$('#customfieldA').change(function() {
showHidefields();
});
}

function showHidefields(){

$('#customfieldB').closest('div.field-group').hide();
if($("#customfieldA option:selected").val() == 'xxx'){
$('#customfieldB').closest('div.field-group').show();
}else{
$('#customfieldB').val('');
$('#customfieldB').closest('div.field-group').hide();
}
}

});
</script>

1 answer

1 accepted

Hi Nick,

This can be achieved using the Behaviours module of the Script Runner plugin.

I have attached below a sample script which can be applied as a behaviour to Select List Field A in order to make Select List Field B visible and required when a certain option is selected in Select List Field A.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
  
// Get pointers to the custom field(s) required
def cf = getFieldByName("Select List A")
def cf1 = getFieldByName("Select List B")

// Get the Value of Select List A
def cfVal = cf.getValue().toString()

// Hide Select List B by default
cf1.setHidden(true)

// If option B is selected in Select List A show Select List B and make it required
if(cfVal == "B"){
    cf1.setHidden(false) // Show Select List B
    cf1.setRequired(true) // Make Select List B Required
}

I hope this helps

Kristian

Thanks a lot Kristian!! That works great!

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 May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

835 views 2 9
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