Detect any change in an EditForm

Home Forums Classic DFFS Detect any change in an EditForm

Viewing 5 reply threads
  • Author
    Posts
    • #30657
      Alexander Bautz
      Keymaster

        I got a request for a snippet that detects any change in an EditForm, but cannot remember if it was in the forum or by email… – in any case, here it is.

        Put this in your EditForm Custom JS:

        var formLoadState = {};
        
        function dffs_ready() {
            setTimeout(function() {
                jQuery.each(spjs.dffs.fieldtype, function(fin, type) {
                    var val = getFieldValue(fin);
                    if ((type === "SPFieldNote" || type === "SPFieldHTML") && val.indexOf("<p>") > -1) {
                        val = jQuery(val).text();
                    }
                    formLoadState[fin] = val;
                });
            }, 500);
        }
        
        function dffs_PreSaveAction() {
            var formSaveState = [],
                changed = false,
                cArr = [];
            jQuery.each(spjs.dffs.fieldtype, function(fin, type) {
                var val = getFieldValue(fin);
                if ((type === "SPFieldNote" || type === "SPFieldHTML") && val.indexOf("<p>") > -1) {
                    val = jQuery(val).text();
                }
                if (String(val) !== String(formLoadState[fin])) {
                    changed = true;
                    cArr.push(spjs.dffs.fieldData[fin].disp);
                }
            });
            if (changed) {
                // Form is changed - do something? - the alert is just an example
                spjs.dffs.alert({
                    "title": "These fields have changed",
                    "msg": cArr.join("<br>")
                });
            } else {
                // Form is not changed
            }
            return true;
        }

        This will collect all the form field values when the form loads, and then compare it with the current value when the user tries to save the form – alerting in case of any change.

        In the case of a field change, you could for example set another field value before save.

        I Hope someone finds this useful.

        Alexander

      • #30667
        Amal Vellappillil
        Participant

          Hi Alex,

          Thank you so much for the snippet.
          Just out of curiosity. What is the difference between the way you have it vs comparing between beforeProperties and getFieldValue?

          spjs.dffs.beforeProperties[fin] !== getFieldValue(fin)

          -Amal Vellappillil

        • #30673
          Alexander Bautz
          Keymaster

            Comparing like you suggest is mostly the same, but beforeProperties changes some of the values (for people pickers, taxonomy fields, lookups and some date fields) – also the comparison of “SPFieldNote” and “SPFieldHTML” needs a bit more. And also, the loading is deferred 500 milliseconds to ensure for example people pickers are fully rendered before the script fires.

            Alexander

          • #30676
            Amal Vellappillil
            Participant

              Thank you for taking the time to explain the difference.

            • #32440
              William Ellis
              Participant

                I have tried this alert code, but the alert message flashes for a split second and disappears, saving the changes. Anyone else had this issue?

              • #32447
                Alexander Bautz
                Keymaster

                  The alert is just an example in the code and will not stop the save. What are you trying to do?

                  Alexander

              Viewing 5 reply threads
              • You must be logged in to reply to this topic.