programatically make custom fields required

John Rassel February 19, 2014

How can I programatically make a custom field that I created (also programatically) required on a given field conifguration screen?

I have looked through the docs and found a few methods that return a boolean if the field is required, but none to set the field as required.

Any help?

2 answers

1 accepted

1 vote
Answer accepted
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.
February 21, 2014

It's a common request with an easy fix, but it's nothing to do with screens or field configurations.

A screen simply says "this field shows when I am used for create/edit/view/transition". It has nothing to say about mandatory fields

A field configuration is totally different. It's got nothing to say about what fields are on a screen, but does tell Jira if a field is a) available to the project/issuetype and b) mandatory through the whole life-cycle.

The "fix" is simply to use a "validator" on transitions to say "field must be filled in on this transition"

John Rassel February 21, 2014

I"m new to all this and am starting to follow your lead and understand it. So, i will start investigating the APIs more to which one will allow me to do that.

How would I handle the issue creation? Since that's not really a transition ?

Thanks

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.
February 21, 2014

Issue create is a transition, just an odd one (no conditions, and no start point). You can add validators to create.

Also, you can make a field required in the field configurations and Jira will require it on issue-create (you just won't be able to blank it out anywhere in the issue lifecycle)

John Rassel February 21, 2014

Thanks Nic. That wont give me the asterix on the screen to notify the user that the field is required until it's posted correct?

I'm also interested in that. Just how you would mark the field required manually through the field config screen as admin, just programatically since these will be created upon plugin installation.

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.
February 21, 2014

A validator will not give you the asterix. The field configuration will.

To do it in the UI, go to admin -> field configurations -> Field configuration, find the field you want to make mandatory and click the mandatory flag. For each one.

Programatically, er, well, don't. You almost certainly don't want to make a custom field mandatory when you install a plugin. Someone who installs such a plugin is going to end up with users screaming that it's unusable, because it'll mess up all their configurations.

If you must do this, then your code *must* do the following:

  1. Iterate through every screen to check if it's an edit, transition (means iterating through the workflows to find them) or create screen. If it is, you'll have to inflict the new field(s) on the screen. You could be quite blunt and simply add them to every screen though. Which is fun when your admins realise they need to unpick this.
  2. Next, you'll need to iterate through every field configuration, find the new field(s) on the list and set the field to mandatory
  3. Third, you'll need to educate every single user that their existing data can't actually be moved on until they've gone into their issues and added data to the new field (although, if you've done step 1, then they will just be forced to enter data on the transition screen)

Seriously, I REALLY wouldn't do this. I certainly wouldn't use a plugin that does it. (Heck, I'd avoid creating custom fields automatically on plugin install). Keep your code simple - *provide* new field types, and document how to add and use them. It only takes an admin a few clicks to add a new field and using their knowledge of their system to require the fields where they're actually needed instead of vandalising the whole thing is what your admins are there for.

John Rassel February 21, 2014

This whole thing will be in isolation to everything within JIRA.

I'm stuck on # 2 on how to set my custom fields (on my own custom screens and issue types and etc ...) as "required" or "mandatory" to get the asterix to indicate the user that the field is required before they even click "submit/create".

I can't find the method call similiar to "setRequired" or "setMandatory" in any of the classes. Which class and what method would I utilize for this operation? I'm past the point of creating the custom field and the field configuration and tied them together. I just need to mark the fields as required now.

I'm using EditableFieldLayoutImpl to create my Field Configuration and right after I can loop through and getall the FieldLayoutItem. Not sure how close or far I'm from achieving what I want.

BTW: thanks for all the feedback and the approach. It's still early and I'm experimenting with this.

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.
February 21, 2014
Ah, i see. You are not inflicting it on the while system, just creating new schemes. As we said already, it's not screens or types to look at, it is just the field configuration. I seem to remember a function on fieldlayouitem that sets/blanks the mandatory flag in the field config, but I haven't coded in that area for ages
John Rassel February 21, 2014
0 votes
John Rassel February 21, 2014

Anyone? Is this even possible?

Donepudi Bhargava October 22, 2018

Hi John. Were you able to code for required in field configurations? If yes, could you guide me with the solution. I have been searching online and in the API for days now.

Suggest an answer

Log in or Sign up to answer