Forums

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

Validating Checkboxes using JMWE

Ricardo Gomes
Contributor
August 21, 2020

Hi,

I have a Jira form using checkboxes, so for each question I basically have:

Form checklist question:

[ ] N/A

[ ] Option 1

[ ] Option 2

[ ] Option 3

 

So upon submission, I need to validate the following:

1) Form can't be submitted empty (w/o selecting any options)

2) Form can't be submitted without selecting either "N/A" or at least one other option from options 1/2/3. 

So we must consider:

- 2.1) IF "N/A" is selected, no other option can be selected and submitted

- 2.2) IF "N/A" is not selected then I have to have at least one other option selected

 

I was able to accomplish #1 above with the following:

(! issue.customfield_1 || issue.customfield_1.length > 0) && (! issue.customfield_2 || issue.customfield_2.length > 0) && so on...

 

Now, to accomplish #2, I've tried the following (1 field example):

(!!issue.customfield_1 && issue.customfield_1.some(it => it.value == "N/A")) && (!!issue.customfield_1 && issue.customfield_1.length = 1)

which is attempting to validate "IF N/A checkbox is selected AND the count on the number of checkboxes select = 1 THEN pass the validation" BUT this doesn't seem to be working...

 

What am I missing? Or what's the best path to validate this?

Thank you!

 

 

1 answer

1 accepted

0 votes
Answer accepted
David Fischer
Community Champion
August 21, 2020

Hi,

for #1, I believe you want this instead:

!! issue.customfield_1 && issue.customfield_1.length > 0 && !! issue.customfield_2 && issue.customfield_2.length > 0

which forces each field to be non-empty.

As for #2, focusing on a single field first, and already knowing from #1 that no field is empty:

issue.customfield_1.some(it => it.value == "N/A") && issue.customfield_1.length == 1
|| !issue.customfield_1.some(it => it.value == "N/A")

Combining the two:

 !! issue.customfield_1 && issue.customfield_1.length > 0 && !! issue.customfield_2 && issue.customfield_2.length > 0
&& issue.customfield_1.some(it => it.value == "N/A") && issue.customfield_1.length == 1
|| !issue.customfield_1.some(it => it.value == "N/A")
&& issue.customfield_2.some(it => it.value == "N/A") && issue.customfield_2.length == 1
|| !issue.customfield_2.some(it => it.value == "N/A")

If you end up with an expression that's too long (Jira expressions are limited to 1000 characters if I remember correctly) , you can always create a validator per field instead (which will also make error messages clearer).

David

Ricardo Gomes
Contributor
August 21, 2020

Thanks, David! 

That worked. I just had an issue with the size but broke it down to several validators which solved the problem.

Cheers!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events