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

Question

Question

Laserfiche Modern Forms - Limiting "Other" field length

asked on February 21

Does anyone have ideas on how to make this JS work in the Laserfiche Forms Modern Designer? It is supposed to limit the "Other" field length to 10 and show a message. 

 

const fields = LFForm.findFields(f => 
	    ["Checkbox", "Radio", "SingleLine", "MultiLine"].includes(f.componentType)
	);
	fields.forEach(f => {
	    let fld = LFForm.findFieldsByFieldId(f.fieldId)[0];
	    LFForm.onFieldChange(() => { ValidateOtherField(f.fieldId); }, fld);
	});
	function ValidateOtherField(checkboxId) {
	    let fld = LFForm.findFieldsByFieldId(checkboxId)[0];
	    let fieldVal = LFForm.getFieldValues({ fieldId: checkboxId });
	    if (fieldVal.otherChoiceValue && fieldVal.otherChoiceValue.length < 10) {
	        let subtext = `<b>${fieldVal.otherChoiceValue}</b> was longer than the maximum number of characters and has been removed.`;
	        // Set the note below the field
	        LFForm.changeFieldSettings(fld, {
	            "subtext": "Note"
	        });
	        // Clear OtherFieldValue, deselect
	        fieldVal.value.pop();
	        fieldVal.otherChoiceValue = "";
	        LFForm.setFieldValues(fld, fieldVal);
	    }

Screenshot:

0 0

Answer

SELECTED ANSWER
replied on February 21
const fields = LFForm.findFields(f => 
    f.componentType === "Checkbox" || 
    f.componentType === "Radio" || 
    f.componentType === "SingleLine" || 
    f.componentType === "MultiLine"
);
fields.forEach(f => {
    let fld = LFForm.findFieldsByFieldId(f.fieldId)[0];
    LFForm.onFieldChange(() => { ValidateOtherField(f.fieldId); }, fld);
});
function ValidateOtherField(checkboxId) {
    let fld = LFForm.findFieldsByFieldId(checkboxId)[0];
    let fieldVal = LFForm.getFieldValues({ fieldId: checkboxId });
    if (fieldVal.otherChoiceValue && fieldVal.otherChoiceValue.length > 10) {
        let subtext = `<b>${fieldVal.otherChoiceValue}</b> was longer than the maximum number of characters and has been removed.`;
        // Set the note below the field
        LFForm.changeFieldSettings(fld, {
            "subtext": subtext
        });
        // Clear OtherFieldValue, deselect
        fieldVal.value.pop();
        fieldVal.otherChoiceValue = "";
        LFForm.setFieldValues(fld, fieldVal);
    }
}

Looks like a bracket was missing at the end.  This one worked for me.

 

0 0
replied on February 21

Thank you it works for me now too!

0 0

Replies

replied on February 21

At least some of this is supported outside of javascript on the modern forms:

On your field, go to the advanced tab and set the pattern match to .{10} 

Now, switch to the "Rules" and set a new "Error Rule".

For mine, I set it to display the error when the field text does not match the required pattern match.  Then I can set a custom message to display.  Here's the result:

This isn't going to do everything the javascript you have does but hopefully it is enough to be usable.

0 0
replied on February 21

Unfortunately, that doesn't apply to the Other field on Checkboxes and Radio buttons.

0 0
replied on February 21

Good call... ignore my answer.

0 0
replied on February 21

Still a good think to know and keep in your back pocket when needed. Thank you!

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

Sign in to reply to this post.