Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to throw two errors on two different fields using custom script validator

SWAPNIL SRIVASTAV
Contributor
November 5, 2019

I want to validate two different fields in a Custom Script Validator and show two different errors on the fields if both the validations fail. 

Currently it is showing only one error, the one I have mentioned in the last

Any help would be appreciated

 

3 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Antoine Berry
Community Champion
November 5, 2019

Hi @SWAPNIL SRIVASTAV ,

You can throw an error on a field within a validator script using : 

throw new InvalidInputException("customfield_11001","This field is mandatory.")

 Then you need to make use of if conditions to check if both fields fail validation.

Antoine

1 vote
PD Sheehan
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.
November 5, 2019

The "throw new InvalidInputException" will interrupt the code so you can only throw one.

So either throw it without specifying the field (and then the error message will be at the top of the form), or you duplicate your code and have 2 separate validators. You can still look at the values for both fields to determine which message to display, but you'll have to point one configuration to show the message on one field and the other configuration for the other field.

0 votes
SWAPNIL SRIVASTAV
Contributor
November 5, 2019

Hello @PD Sheehan and @Antoine Berry ,

Thank you for the responses.

throw new InvalidInputException("customfield_11001","This field is mandatory.")

This worked for me. I applied two if conditions for checking two fields in a single validator and each if block had one throw statement. It is showing error on screen properly.

The only  problem is that if both validation fail, it will show the first error first and if you enter the correct value there, then the error of the second field will be shown. It wont show both errors at the same time. Is there any way to do that?

Thanks and Regards,

Swapnil Srivastav.

Antoine Berry
Community Champion
November 5, 2019

Hi @SWAPNIL SRIVASTAV ,

You should add a validator on each field checking for both field values, i.e. something like 

if (value1 == null && value2 != null) {
throw new
InvalidInputException("customfield_11001","This field is mandatory.")
}
else if (value1 == null && value2 == null) {
throw new InvalidInputException("customfield_11001","This field and field 2 are mandatory.")
}

And have something similar on field 2.

SWAPNIL SRIVASTAV
Contributor
November 6, 2019

Hi @Antoine Berry ,

Thank you. That works. 

Best Regards,

Swapnil Srivastav.

Like Antoine Berry likes this
Queenmary February 9, 2021

Hi Swapnil,

I am also in the same need to display two error message at a same time in the jira screen.

If that works good for you can you please provide the code snippet below that will be a great help for me.

 

Regards,

Queenmary Bastin

Like Antoine Berry likes this
Queenmary February 9, 2021

Hi Antoine,

Can you help me on the above query...

 

Regards,

Queenmary Bastin

Like Antoine Berry likes this
TAGS
AUG Leaders

Atlassian Community Events