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

Question

Question

Make comment field mandatory only when approver selects "Reject" (optional on "Approve") - Laserfiche Form

asked on September 10

Hello community! On my approval form, approvers have two options: Approve or Reject. I'd like to make the comment field conditional — optional when the approver selects Approve, but mandatory when they select Reject.

How can I set this up?

0 0

Replies

replied on September 11

This works in the Layout Designer on Forms Version 12.0.2509.20409

  1. Add a Single Line field to your form.  Set its variable to: user_submit_action
  2. Add this JavaScript to your form: 
    //Add the submit action to the user_submit_action field on submission.
    LFForm.onFormSubmission(async function (event) {
      await LFForm.setFieldValues({variableName: "user_submit_action"}, event.data.action.value);
    });
    
  3. Add these two field rules to your form:

 

The comments box is now required if the user clicks Reject, but not if they click other buttons.

3 0
replied on September 30

This also works with the Cloud! If you change the Button Label, use the Button CSS Class for the second rule. 

0 0
replied on September 10

I'm not sure if there is currently a way to validate the Task Comments Box; there WAS a way in the old forms designer, but I haven't been able to figure out how to read it with the new JavaScript approach.

 

An alternate solution could be to hide the default comment box and add your own, which actually makes it easy since this is one of the example use cases in the documentation!

Documentation here, the onFormSubmission example is where I pulled the code from:

https://doc.laserfiche.com/laserfiche/en-us/content/pa-bp-build-form-scripts-lfform-events.htm

 

This rule hides the default box:



And this JavaScript requires a new comment box to be non-empty if "Reject" is chosen:
The only mandatory change would be to replace variableName: "Comments" with whatever your comments box variable name is.

LFForm.onFormSubmission(async function (event) {
// Get the value of the clicked submission button
const userAction = event.data.action.value;
const approvalComments = LFForm.getFieldValues({ variableName: "Comments" });
if (userAction === "Reject" && approvalComments === "") {
return {error: "Please add comments in order to reject."};
 }
});

 

You do get a bit of unwanted white space between the box and the buttons, but there may be a way to minimize that with some CSS.

 

If someone else has a way to reference the existing comments box then that would be great, but this is how I would work around the problem!

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

Sign in to reply to this post.