You are viewing limited content. For full access, please sign in.

Question

Question

How to Refer to and Set a single Radio Button in Modern Form Designer, based on Field Value?

asked on July 26, 2023

Hi I am making what should be a simple form . I have a table with rows labeled "Week 1" thru "Week 5",  built in modern forms designer in the Laserfiche Cloud. How do I set the selection to "selected" either in Javascript or through the interface? I want to do so based on the Field Value of another field.

All the existing answers that I have found that were remotely close are for the old forms in JQuery. I just need the syntax for setting a radio button to "selected" in an "If Statement" based on a Field Value.

 

0 0

Answer

SELECTED ANSWER
replied on July 27, 2023

You are right that the other examples created for the Classic Designer are not going to work in the New Designer.  The solutions in the classic designer are designed to use JQuery to manipulate the attributes of the fields directly.  But the New Designer doesn't use JQuery, and cannot access the field attributes directly.

Luckily, something like this is possible using the LFForm interface that is built into the New Designer.

The following code is assuming that you have a radio button with class name of triggerRadio and values of A, B, and C.  It also assumes you have a radio button with class name of targetRadio and values of 1, 2, and 3.

Using options from the LFForm interface, it grabs the first field that it finds with the triggerRadio and targetRadio classes, and then monitors for changes to the triggerRadio field and uses that to update the value of the targetRadio field.  

var triggerField = LFForm.findFieldsByClassName('triggerRadio')[0];
var targetField = LFForm.findFieldsByClassName('targetRadio')[0];

//When the triggerField is changed, check
//for its value and change the targetField
//accordingly.
LFForm.onFieldChange(function(){ 
  var selectedTrigger = LFForm.getFieldValues(triggerField);
  if(selectedTrigger.value == 'A') {
    LFForm.setFieldValues(targetField, {value: "1"});
  }
  else if(selectedTrigger.value == 'B') {
    LFForm.setFieldValues(targetField, {value: "2"});
  }
  else if(selectedTrigger.value == 'C') {
    LFForm.setFieldValues(targetField, {value: "3"});
  }
}, triggerField);

 

Note that this is optomized for radio buttons, where only a single value is marked at a time.  If you were to try to use this with checkboxes, where multiple values could be selected, it wouldn't work without modifications because line 8 of the code would be getting an array of values instead of a single value, and the if statements that follow wouldn't work in that case.  We would need to add code to check if it was an array or a single value, and proceed differently based on the result.

3 0
replied on November 9, 2023

Hey @████████ I am struggling with this code. I have a radio button (Field 213) that when it equals yes. I must copy a Field 12 into Field 15. What am I missing?

LFForm.onFieldChange(function() {
    // Check if the radio button with ID 213 has a value of "Yes"
    if (LFForm.getFieldValues({ fieldId: 213 }) === 'Yes') {
        // If the condition is true, set the values of field 15 to the values of field 12
        LFForm.setFieldValues({ fieldId: 15 }, LFForm.getFieldValues({ fieldId: 12 }));
    }
}, { fieldId: 213 });

 

0 0
replied on November 9, 2023

Nevermind! I think I have it. Whew.

1 0

Replies

replied on July 27, 2023

Matthew, I gotta say, that is one of the clearest best answers I've ever seen on a tech message board. THANK YOU!

2 0
replied on July 27, 2023

haha, I'm glad you like it.

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.