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

Question

Question

JavaScript giving error in modern form designer

asked on March 25

I have JavaScript code to take a number associated with a radio button, populate it in a field and then make that field read only.

 

////////////////////////////////////////////////////////////////////////

//  Script for making all target fields read-only in Laserfiche Forms

//

//  This script wires up source->target field mappings such that all target

//  fields become read-only. Each target field is disabled on load and is

//  updated via an enable->set->disable cycle when the corresponding source field changes.

//

//  Make sure the target fields are NOT marked "read-only" in the designer.

////////////////////////////////////////////////////////////////////////

 

/**

 * main function for mapping source->target fields.

 * All target fields are treated as read-only.

 */

function setupFieldMapping(sourceFieldVar, targetFieldVar) {

  // Disable the target field on load

  LFForm.disableFields({ variableName: targetFieldVar })

    .catch(err => console.error("Error disabling on load:", targetFieldVar, err));

 

  // Subscribe to changes on the source field

  LFForm.onFieldChange(function() {

    // 1) Grab the raw value from the source

    let rawValue = LFForm.getFieldValues({ variableName: sourceFieldVar });

   

    // 2) Convert rawValue into a string for the regex

    if (Array.isArray(rawValue)) {

      rawValue = rawValue.length ? String(rawValue[0]) : "";

    } else if (rawValue && typeof rawValue !== "string") {

      // e.g. could be a number, or { value: "some text" }

      if (typeof rawValue === "number") {

        rawValue = String(rawValue);

      } else if (rawValue.value) {

        rawValue = String(rawValue.value);

      }

    }

   

    // 3) Extract the digits in parentheses at the end, e.g. "(5)"

    const match = (rawValue || "").match(/\((\d+)\)\s*$/);

    if (match) {

      // We found a number in parentheses, e.g. "5"

      // To avoid "Invalid values," the extracted value must match the actual field type in your Form Designer.

      const extractedNumber = parseInt(match[1], 10); 

 

      // Always perform the enable->set->disable cycle to update the read-only field

      LFForm.enableFields({ variableName: targetFieldVar })

        .then(() => LFForm.setFieldValues({ variableName: targetFieldVar }, extractedNumber))

        .then(() => LFForm.disableFields({ variableName: targetFieldVar }))

        .catch(err => console.error("Error updating readOnly target:", targetFieldVar, err));

    }

    // else no match => do nothing

  }, { variableName: sourceFieldVar });

}

 

// ----------------------------------------------------------------------

// Wire up each source->target pair. All target fields will be read-only.

// ----------------------------------------------------------------------

setupFieldMapping("JKDescription",        "JKRange_1");

setupFieldMapping("QWDescription",        "QWRange");

setupFieldMapping("QuantWorkDescription", "QuantWorkScore1");

setupFieldMapping("MSDescription",        "MSScore1");

setupFieldMapping("CISDescription",       "CISScore_30");

setupFieldMapping("STDescription",        "STScore30");

 

Everything worked as expected in Preview mode. When I tried to run through a live example I get an error. 

 

Any help would be appreciated. Thank you.

0 0

Replies

replied on March 25 Show version history

There isn't anything obvious missing in your code, you're pulling the matched number from the string correctly. In fact, I put together a quick process that mapped a single line and a radio button to numeric fields and I was able to submit just fine.

The error message references "30 Day Weighted Score". Is there a formula being used to populate this field?

0 0
replied on March 25

There was. I removed all formulas and still got the same result. 

0 0
replied on March 25

Without any formulas that field should be empty though right?

0 0
replied on March 25

Yes, it is.

0 0
replied on March 25

What is the new error without formulas?

 

0 0
replied on March 25

The screen shot above is with the formulas entered.

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

Sign in to reply to this post.