Jump to content

Conditional 'Required' Field Using And AND Or Logic


Recommended Posts

As far as I understand, you cannot use both and AND or logic in the rules portion of a datapage to make a field required, however that is what I am trying to do. I am fairly new to JavaScript, and also don't completely understand all the particularities of how Caspio references different elements. I have written out the following script in an attempt to make a field required based on certain criteria, but this does not seem to work as expected. Any help would be appreciated!

To give more background on the app and the overall goal, this is a performance evaluation application, and the attempt is to require a comment if a certain evaluation score is selected. For example, the organization can declare that any score of 1 must require a comment to go along explaining why a score of 1 was given, etc.

<script> 
  let Q1C = document.querySelector('[id*=cbd_JPT_Performance_Eval_Q1_Scr#]');
  if ((Q1C.value == '1' && '[@field:a_JPT_Organizations_Comment1_Req^]' == 'TRUE') ||
      (Q1C.value == '2' && '[@field:a_JPT_Organizations_Comment2_Req^]' == 'TRUE') ||
      (Q1C.value == '3' && '[@field:a_JPT_Organizations_Comment3_Req^]' == 'TRUE') ||
      (Q1C.value == '4' && '[@field:a_JPT_Organizations_Comment4_Req^]' == 'TRUE') ||
      (Q1C.value == '5' && '[@field:a_JPT_Organizations_Comment5_Req^]' == 'TRUE')) {
    document.getElementById("EditRecordd_JPT_Performance_Eval_Q1_Comment").required = true;
  }
</script>
 

Link to comment
Share on other sites

Hello @Connonymous,

It looks like you can do this without JavaScript but by using the Calculated Value.

1) Add a Virtual field that is set to the  Calculated Value, and add the formula:

CASE 
WHEN 
([@field:cbd_JPT_Performance_Eval_Q1_Scr] = 1 AND [@field:a_JPT_Organizations_Comment1_Req] = 1)
OR
([@field:cbd_JPT_Performance_Eval_Q1_Scr] = 2 AND [@field:a_JPT_Organizations_Comment2_Req] = 1)
OR
([@field:cbd_JPT_Performance_Eval_Q1_Scr] = 3 AND [@field:a_JPT_Organizations_Comment3_Req] = 1)
OR
([@field:cbd_JPT_Performance_Eval_Q1_Scr] = 4 AND [@field:a_JPT_Organizations_Comment4_Req] = 1)
OR
([@field:cbd_JPT_Performance_Eval_Q1_Scr] = 5 AND [@field:a_JPT_Organizations_Comment5_Req] = 1)
THEN 1
ELSE 0
END

Double-check the formula logic (field names and values). 

The idea is to return '1' if the condition is met and '0' if the condition is not met. You may use different values, it doesn`t matter. This value should be used in the Rule.

2) Create a Rule like "If  Virtual field = 1 (in this example), make the field required"

dyacgfh.png

Link to comment
Share on other sites

@CoopperBackpack Ah, thank you, that's a good idea! It seems to be working as expected. Slight delay after clicking one of the options that is not present when only checking the criteria through standard (non-virtual) fields though. Is there any way around this, or is checking the multiple criteria likely just going to lead to some delay?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...