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

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published Jul 25, 2018 in Marketplace Apps

Jira Cloud and Bitbucket Cloud Integration with Microsoft Teams

One of the newest products in the Microsoft family - Microsoft Teams,  is a chat-based hub for teamwork that integrates all the people, content, and tools your team needs to be more engaged and ...

733 views 0 3
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