Is there a way to make a custom field required based on Status?

Amanda Gadrow January 9, 2012

We'd like to be able to make a field required only when the ticket is marked as "Resolved"; it does not need to be required when the ticket is created. Is there a way to do this, either natively or through scripting?

2 answers

1 accepted

0 votes
Answer accepted
Jeff Kirby
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, 2012

A clever hacky-ish way to do it is to include Javascript in the description of the custom field in the field configuration. For example, here's what we use to make sure that a custom field called Resolution Notes is filled out depending on the values of a cascading select called Resolution Reason

*Field required.  Please refer to <a href="http://x.y.com/display/ES/JIRA+Resolution+Reasons" target="_blank">Resolution Reason Descriptions</a> for more details.

<script language="javascript">
   jQuery(function() {
      var getFieldIdByName = function(name) {
         return jQuery("label:contains('" + name + "')").attr("for");
      }
      var id = getFieldIdByName('Resolution Reason');
      var first = jQuery("#" + id);
      var second = jQuery("#" + id + "\\:1");
      if(first.length && second.length) {
         var dialog = first.closest("div[id$='-dialog']").keypress(function(event) {
            // disable Enter key
            if(event.which == 13) {
               event.preventDefault();
            }
         });
         var screen = document.URL.indexOf("CommentAssignIssue") >= 0;
         if(dialog.length || screen) {
            first.css("display", "none");
            var title;
            var getTitle = function(s, c) {
               var title = jQuery.trim(s.substring(0, s.indexOf(c)));
               if(title == "Shortcut to Verify") {
                  title = "Implemented";
               }
               return title;
            }
            if(screen) {
               title = getTitle(document.title, "[");
            }
            else {
               title = getTitle(dialog.find("h2.dialog-title").text(), ":");
            }
            first.find("option:contains('" + title + "')").each(function() {
               var option = jQuery(this);
               if(option.text() == title) {
                  option.attr("selected", "selected");
               }
            });
            first.change();
            if(title == "Not Reproducible") {
               second.siblings("div.description").append('<p><table border=1><tr><td><font color="#ff0000"><b>If the bug is reliably reproducible on the build in which it was found but is not exhibited on subsequent builds then the bug should be closed as Implemented with Mystery Fix as the cause of the fix. If this is the case please close this bug as Implemented.</b></font></td></tr></table></p>');
               second.find("option:last").attr("selected", "selected");
            }
            first.closest("form").find(":submit").click(function(event) {
               var missing = new Array();
               if(second.attr("selectedIndex") == 0) {
                  missing.push('Resolution Reason');
               }
               var notes = jQuery("#" + getFieldIdByName("Resolution Notes"));
               if(notes.length && !jQuery.trim(notes.val()) && (title == "Implemented" || title == "Will Not Implement")) {
                  missing.push('Resolution Notes');
               }
               if(missing.length == 1) {
                  alert('Please complete the ' + missing[0]);
               }
               else if(missing.length > 1) {
                  alert('The following fields are required: ' + missing.join(" and "));
               }
               if(missing.length > 0) {
                  event.preventDefault();
               }
            });
         }
      }
   });
</script>

1 vote
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 9, 2012

The usual way to do it is add a plugin that enables "make field mandatory on transition". There's plenty of them around. They're a better way to do it than javascript because it's a doddle to bypass javascript tricks, but quite complex to implement if you have cases where you only want a field mandatory in certain specific circumstances.

Jeff Kirby
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, 2012

I agree, Nic. We usually use the two together, come to think of it.

Suggest an answer

Log in or Sign up to answer