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

Question

Question

Modern Designer Javascript

asked on April 30, 2024 Show version history

I am developing a form process with several fields, each accompanied by a secondary hidden field for validation purposes. The workflow automatically populates these hidden fields, and I want to implement a warning system that alerts the user if their input does not match the workflow's output. Currently, I'm using field rules combined with a custom HTML activity to display a warning when both primary fields are filled but do not match (i.e., field1 != field2). However, I'm encountering an issue where the warning is triggered prematurely—before the user finishes inputting their data in field1. How can I adjust the timing of the rule so that the warning only appears after the user has fully entered their data?

I have been using some basic javascript in the classic designer but cannot get it working in the modern designer, any assistance would be greatly appreciated.

$(document).ready(function() {
  function compareFields() {
    $('.Submit').show();
    $('.warningText').remove();

    var field1 = $('.field1 input').val();
    var field2 = $('.field2 input').val();

    // Check if field 2 has a value
    if (field2 !== '') {
      // Compare Field 1 and Field 2 values
      if (field1 !== field2) {
        $('<p class="warningText"><font color="red">Field 1 and Field 2 do not match.</font></p>').insertAfter('.field2 input');
      }
    } else {
      $('<p class="warningText"><font color="red">Field 2 must not be empty.</font></p>').insertAfter('.field2 input');
    }
  }
  
  // Trigger the comparison when Field 1 or Field 2 lose focus
  $('.field1 input, .field2 input').blur(compareFields);
});

 

0 0

Answer

SELECTED ANSWER
replied on May 1, 2024 Show version history

Hello,

JavaScript was changed significantly in the Forms Layout Designer.

First, jQuery is no longer imported by default so you would either need to use vanilla JavaScript or import the jQuery library via JavaScript.

Second, the JavaScript is now sandboxed meaning it cannot interact directly with the page elements. Any interactions must be done through methods available in the LFForm object, so it is not possible to manually show/hide elements or insert HTML content.

Based on your code and what's available in the LFForm object, I don't think there's currently a way to replicate the exact same functionality in the new designer.

However, you could probably get the error messages working without JavaScript by utilizing multiple Custom HTML fields (instead of dynamically changing the content) in combination with the more advanced Field Rule options available in the new layout designer.

 

As for using the code in the Classic Designer, I would try attaching the event handler to the change event rather than the blur event so it only gets triggered on a change rather than any time the field loses focus.

Additionally, I would move your compareFields method out of the document ready event.

Something like this:

$(document).ready(function() {
  // Trigger the comparison when Field 1 or Field 2 lose focus
  $('.field1 input, .field2 input').on('change', function () {
        compareFields();
  });
});

function compareFields() {
    $('.Submit').show();
    $('.warningText').remove();

    var field1 = $('.field1 input').val();
    var field2 = $('.field2 input').val();

    // Check if field 2 has a value
    if (field2 !== '') {
      // Compare Field 1 and Field 2 values
      if (field1 !== field2) {
        $('<p class="warningText"><font color="red">Field 1 and Field 2 do not match.</font></p>').insertAfter('.field2 input');
      }
    } else {
      $('<p class="warningText"><font color="red">Field 2 must not be empty.</font></p>').insertAfter('.field2 input');
    }
}

 

1 0
replied on May 1, 2024

Thank you for answering this.

0 0

Replies

replied on May 1, 2024

Just an FYi, you can use LFForm to change the text in an HTML field on the page, so that you don't have to have multiple HTML fields but to Jason's point, a Field Rule would likely be easiest.

2 0
replied on May 2, 2024

Hi Robert

Just so you have some context, this is what you code would look like in the Modern Designer.

I just did a rough translation so it's not as good as it could be, but would provide similar functionality. i'd still try and use Rules, etc before getting into the JS when possible
 

LFForm.onFieldBlur(function() {

    var field1 = LFForm.getFieldValues({fieldId: 1});
    var field2 = LFForm.getFieldValues({fieldId: 2});
  console.log(field1);
  console.log(field2);
      // Check if field 2 has a value
  if (field1 == field2) {
  LFForm.changeFieldSettings({fieldId: 3}, {content: ""});
      }
  if (field1 !== field2) {
  LFForm.changeFieldSettings({fieldId: 3}, {content: "Field 1 and Field 2 do not match"});
      }
  if (field2 == "") {
  LFForm.changeFieldSettings({fieldId: 3}, {content: "Field 2 must not be empty."});
    }
   
}, {fieldId: 1,fieldId: 2});

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

Sign in to reply to this post.