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

Question

Question

Clear a field when form is submitted

asked on May 7, 2024

Hello, I am using the following JS code to clear my field when the form is submitted because i the field contains invalid input.

LFForm.onFormSubmission(function () {
    var fieldsToClear = LFForm.findFieldsByClassName("clearThisField");
    LFForm.setFieldValues(fieldsToClear, "");
      
});

 

I cant get rid of this field because i need the content in the process. Using this code when I hit submit it clears the field but gives me an error and then when i hit submit the second time it submits the form because this time the field has already been cleared. What can I do for all this to happen in just one time? 

 

Thank you for helping

0 0

Answer

APPROVED ANSWER
replied on May 8, 2024 Show version history

The LFForm.setFieldValues function returns a promise so the actual action is not guaranteed  to happen before form submission. The onFormSubmission function only supports synchronous callbacks right now which explains the behavior you are seeing. You could try the following code, but I doubt you would experience what you are requesting. That being said, this is a valid use case so I've created #521888 to handle asynchronous callbacks in the event functions in the LFForm object.
The onFormSubmission function should accept the async callback, please try the code below and let me know if it works for you.

LFForm.onFormSubmission(async function () {
    var fieldsToClear = LFForm.findFieldsByClassName("clearThisField");
    await LFForm.setFieldValues(fieldsToClear, "");
});

EDIT: I actually confirmed with the team, this handler should accept the async callback so this code should work for you! I will make sure the documentation gets updated to reflect that

2 0

Replies

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

Sign in to reply to this post.