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

Validate checkbox using groovy-scriptrunner

THOMAS SEBASTIAN March 26, 2015

I'm trying to validate the value of a checkbox using behaviour plugin but the custom field behave oddly.  What I'm trying to do is to show/hide other custom fields based on the selection on the checkbox.  So for example if I have 3 options in the checkbox, then each option will reveal/hide another custom field as they are ticked/unticked.

I wrote a simple groovy script behind the checkbox that consist of 3 if statements using contain (as explained here) for each option.  However the script only seems to work when only 1 option is selected.  So when I choose option A the script works fine, but when I choose B afterwards the second if statement doesn't fire.  However if I uncheck A and leave B on the script works.  It is the same with the other combination.

When I tried to display the value of the selected checkbox using setHelpText, only the first option is shown.  Is there something wrong with the logic of my script or is that the expected behaviour of checkbox custom field (i.e. the selected value doesn't stack, only the first one is considered)?

6 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
THOMAS SEBASTIAN March 30, 2015

Hmm that behaviour doesn't apply to me. What I tried to do is display the ticked value of the custom field using behaviour script (setHelpText) and it only display one value at a time. Can you confirm what is showing in yours? Does it mean that when more than 1 option ticked the value will be appended?

0 votes
Udo Brand
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.
March 30, 2015

I doubt that. If I use a condition like cfValues['my Field']*.value.contains("Value2"), it works also if there are several values beside "Value2" ticked.

0 votes
THOMAS SEBASTIAN March 30, 2015

That's alright Udo, appreciate your effort :) Maybe you can help me with the following: I guess my question is partially about the behaviour of checkbox field. The reason the above code is not working as it should is because the checkbox field only recognize the value when there is only one option is ticked. Is this a bug or the expected behaviour of checkbox custom field?

0 votes
Udo Brand
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.
March 30, 2015

Since I'm not the scripting expert. I can't help you at the moment. I guess other can do so, especially with this code shared.

0 votes
THOMAS SEBASTIAN March 29, 2015

Hi Udo, My script is really simple: FormField cfFormA = getFieldById("cfA") FormField cfFormB = getFieldById("cfB") FormField cfFormC = getFieldById("cfC") if (getFieldById(getFieldChanged())*.value.contains("Value A")) { cfFormA.setHidden(false) } if (getFieldById(getFieldChanged())*.value.contains("Value B")) { cfFormB.setHidden(false) } if (getFieldById(getFieldChanged())*.value.contains("Value C")) { cfFormC.setHidden(false) } And set all the fields as hidden before. I assume that the script will be invoked every time there is a change to the checkbox e.g. if there is an additional option selected however it seems to only works when there is only ONE option selected at a time. I'm wondering if this is the default behaviour or perhaps only happened in mine?

0 votes
Udo Brand
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.
March 26, 2015

It would help if you show your script!

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events