Skip to main content
Participating Frequently
December 12, 2022
Answered

Setting a required field based on a dropdown selection

  • December 12, 2022
  • 1 reply
  • 12218 views

So I've seen a few similar topics but nothing I've been able to try/modify that fit what I need so hoping someone can help me fix this:

  • I have a dropdown menu with 7 options
  • 2 of the 7 options need to make a separate text field REQUIRED
  • If any of the other 5 options are selected the separate field text remains OPTIONAL
  • To try to simplify this I set the Export Values of the 2 required items to REQ and the remainder to X

 

Here's what I have so far:

var spod = this.getField("Dept");

if (event.value=="Req") {

spod.required = true;

}

else if (event.value=="X") {

spod.required = false;

}

 

but each time I select either of the req options the dept field doesnt change. What am I doing wrong?

This topic has been closed for replies.
Correct answer Nesa Nurani

sorry to clarify the field is titled SpoDoM but I changed it to target in my stripped out version. I didn't realie I missed that one so can you clarify where that was? Specifically where I should put that second code. I can add a revised version if that helps


Use script in Dropdown field under 'Validate' tab → 'Run custom validation script'.

If the field name is "TARGET" then just replace "SpoDoM" with "TARGET":

var target = this.getField("TARGET");
target.required = event.value == "Req" ? true : false;
target.readonly = event.value == "Req" ? false : true;

 

If you still have trouble, please share your file.

1 reply

Nesa Nurani
Community Expert
December 12, 2022

Since you set export value you need to use script as calculation not validation.

Participating Frequently
December 12, 2022

Thanks, scripts are pretty foreign to me, how would I go about that?

Nesa Nurani
Community Expert
January 19, 2023

Thanks, so does this look correct? I left the terms as they were intended for clarity (SpoDoM, not target). I took out the second script so I just have the one that you provided. Its not doing anything immediatly when I make a selection but maybe I'm not doing it right. Also I noticed that in both instances you had the Req but go from true : false to false : true. Should one of those be X or is there a reason they're the same? SInce I dont know js I'm trying to understand a bit better


Since you have export values set, move the script to 'Calculate' tab as 'Custom calculation script' and it will work.

They are not the same, one line is to set field to required and other is for readonly.