Removing the "none" option from a select field

If you are a JIRA plugin developer, you must have come across this feature request before. Some people just don't like the none option in the select fields for various reasons. One reason, obviously, is to force the users to select a valid value.

How to do it...

There are two ways to hide the none option on the client side, using a JavaScript hack, or on the server side, by modifying the velocity templates that renders the select field. Let us see both, in different contexts.

First, we will see how to hide the none option on a given custom field with the ID 10000. All we have to do here is to add the following JavaScript snippet in the description of the field. As mentioned in the previous recipe, make sure the description is edited in the appropriate field configuration, if applicable:

<script type='text/javascript'> 
  JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) { 
    hideNone(); 
  }); 
  AJS.$(document).ready(function () { 
    hideNone(); 
  }); 
  function hideNone(){ 
    AJS.$("#customfield_10000 option[value='-1']").remove(); 
  } 
</script> 

Tip

We need to make sure the snippet is executed on the NEW_CONTENT_ADDED event as well, to handle inline edits.

Once this snippet is added, the none option will be hidden when the custom field with ID 10000 is edited.

Now, let us see how we can modify the velocity templates to do the same thing. Select Field is a system custom field that uses velocity templates to render the view and edit screens. In order to remove the none option, we need to modify the edit template.

Note that editing the template will remove the none option from all custom fields of type select. If you want to remove it from only certain select custom fields, don't forget to add extra checks in the velocity templates to do the same.

For any system custom field, you can find the associated classes and their velocity templates from the system-customfieldtypes-plugin.xml file, residing under the WEB-INF/classes folder.

In our case, we can find the following snippet related to select-field:

<customfield-type key="select" name="Select List (single choice)" 
 i18n-name-key="admin.customfield.type.select.name" 
 class="com.atlassian.jira.issue.customfields.impl.SelectCFType"> 
    <description key="admin.customfield.type.select.desc">
     A single select list with a configurable list of options.
    </description> 
 
    <resource type="download" name="customfieldpreview.png" 
      location="/images/customfieldpreview/select.png"> 
        <param name="source" value="webContextStatic"/> 
    </resource> 
 
    <resource type="velocity" name="view" 
      location="templates/plugins/fields/view/view-select.vm"/> 
    <resource type="velocity" name="edit" 
      location="templates/plugins/fields/edit/edit-select.vm"/>
    <resource type="velocity" name="xml" 
      location="templates/plugins/fields/xml/xml-select.vm"/> 
 
    <category>STANDARD</category> 
</customfield-type> 

As evident from the preceding snippet, the edit template for the select field is templates/plugins/fields/edit/edit-select.vm. That is the file we need to modify.

All we need to do now is to navigate to the file and remove the following lines:

#if (!$fieldLayoutItem || $fieldLayoutItem.required == false)
  <option value="-1">$i18n.getText("common.words.none")</option>
#else
  #if ( !$configs.default )
    <option value="">$i18n.getText("common.words.none")</option>
  #end
#end

The remaining code in the template must not be deleted. Restart JIRA to make the change effective.

Note

The same approach can be used to remove the none option from other fields, such as radio buttons, multi select, cascading select, and so on. The actual code to remove will differ, but the approach is the same.

There's more...

There's more to it...

Reloading velocity changes without restart (auto reloading)

You can configure JIRA to reload the changes to velocity templates without a restart. To do this, you need to make two changes to the velocity.properties file under WEB-INF/classes:

  1. Set the class.resource.loader.cache property to false. It is true by default.
  2. Uncomment the velocimacro.library.autoreload=true property. This can be done by removing the # at the beginning of the line.

Restart JIRA and the changes to the velocity templates will be reloaded without another restart!

See also

  • The Changing the size of a text area custom field recipe in this chapter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.89.34