Home › Forums › Classic DFFS › Detect any change in an EditForm
- This topic has 5 replies, 3 voices, and was last updated 3 years, 10 months ago by Alexander Bautz.
-
AuthorPosts
-
-
June 14, 2020 at 08:53 #30657
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
-
June 15, 2020 at 15:19 #30667
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
-
June 15, 2020 at 20:35 #30673
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
-
June 15, 2020 at 21:19 #30676
Thank you for taking the time to explain the difference.
-
January 12, 2021 at 14:05 #32440
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?
-
January 12, 2021 at 17:03 #32447
The alert is just an example in the code and will not stop the save. What are you trying to do?
Alexander
-
-
AuthorPosts
- You must be logged in to reply to this topic.