Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

1 vote
Answer accepted
Antoine Berry Community Leader Nov 05, 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

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.

Hello @Peter-Dave 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 Leader Nov 05, 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.

Hi @Antoine Berry ,

Thank you. That works. 

Best Regards,

Swapnil Srivastav.

Like Antoine Berry likes this

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

Hi Antoine,

Can you help me on the above query...

 

Regards,

Queenmary Bastin

Like Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

545 views 15 18
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you