Developing custom field plugin

Hello, dear developers!

I'd like to set my custom field required and as i know there're 2 ways: make it required via Field Configuration Scheme or to make a Validation of this field. The first way is not possible. So the question is : Is it aviable to set validation in customfield which extend TextCFType class? Or are there any methods which can make field required for Project or for Issue?

Best wishes, Andrew

10 answers

1 accepted

3 votes

If you want to handle validation during editing, validateFromParams is indeed the method.

but i dont now how to set conclusion on CF value

What do you mean by this?

OMG i ment condition of course))))

public void validateFromParams(CustomFieldParams relevantParams, ErrorCollection errorCollectionToAddTo, FieldConfig config)
    {
    	
    	CustomField customField=config.getCustomField();
    	
    	
    	String selectedString = (String)relevantParams.getFirstValueForNullKey();
        if(!(StringUtils.isNotBlank(selectedString) && !"-1".equals(selectedString)))

    	{
    			
                errorCollectionToAddTo.addError(customField.getId(), "This custom field is required.");
                

        }
    }

Now it shows message when CF is empty
But strange! When CF is filled with some value pressing on "update" button nothing happens.
No error message and no updating CF values.

That should be something to do with your edit template. Do you have the required headers, with custom field id etc, in the edit template?

Yes, i do.

#if($maName!="ThisIsSystemMaNameWhichShowsThatThisFieldDoNotExistsInThatProject")
#customControlHeader ($action $customField.id $maName $fieldLayoutItem.required $displayParameters $auiparams)

<select class="select" name="$customField.id" id="$customField.id">
    
        <option value="")>$i18n.getText("common.words.none")</option>
    
    #foreach ($cfValue in $cfValues)
        
            <option#if ($value && $value == $cfValue) selected="selected"#end value="$cfValue">$cfValueEncoder.encodeForHtml($cfValue)</option>
        
    #end
</select>

#customControlFooter ($action $customField.id $fieldLayoutItem.fieldDescription $displayParameters $auiparams)
#end

I changed customfield display name to maName. All other things i left as default. And yes, my customfild type extends TextCFType, just i needed some customization

I change $maName to $customField.name but changes still don't apply

Put the header and footer outside of if loop and see if it helps.

Unfortunatly it was useless(((

If it may help until i insert validation, everything worked fine.

Not sure what is going wrong. There must be some error! Why are you getting the field in the method?

I' sorry, Jobin, but i don't understand your question...

Never mind. You need it for adding to the errorCollection.

Call this so that you know if you are missing any other validations.

super.validateFromParams(customFieldParams, errorCollection, fieldConfig);

after inputing other value

no progress((( no any error messages(( this screenshot shows that validation works


I am afraid I don't have anything new to try!

Jobin, firstfull my validation worked if only "None" option value is "-1".
On the other hand, it began to work whe in if clause was ("-1".StringUtols.equalsTo(selectedString))

Now everything is okey. Thank you very much, gentlemens!

0 vote

Where do you need the field as required? In a workflow? or in one purticular step while ediing the issue? While creating?

0 vote

The "field is mandatory" validators you can find in several plugins (e.g. Jira Suite Utilities) will validate any custom field, you won't need to worry about it.

Nic, looks like this plugin adds validation in a workflow

Jobin, Nic, much thanks for your response
Jobin, only while editing and creaeting the issue.
Nic, thank you, i will try.

So, i tried ti validate it. validateFromParams method works but i dont now how to set conclusion on CF value. PLease help me if you can)

0 vote

I'm not sure what you are doing, could you explain where you're using that call and give us a brief outline of what you have set up, and what you are trying to do?

Simple text customfield. I'm trying to set it's behaviour in plugin. Now o need to set it as required. in class MyCF.java i override method validateFromParams. to set validation which will stop user when he will try to apply issue editing when this CF is empty.

0 vote

Ah, I missed your previous comment about validating in workflow, rather than field.

Which validateFromParams method are you using? I mean, which class is it coming from or what have you imported to provide it in your code?

(also what does "set conclusion on CF value" mean? Validation should be a simple "valid" or "not valid, here's an error" type process)

Nic, i explained below. And added some code

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 yesterday in Jira

Mission-critical battery manufacturer fulfills FAA software requirements with Commit Policy Plugin

EaglePicher Technologies is a leading manufacturer of battery systems for diverse industries like defense, aviation, space or medical. As they operate in highly regulated industries, keeping a clear ...

143 views 0 2
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