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.