Field validation

Forums Dynamic Forms for SharePoint Field validation

Viewing 6 reply threads
  • Author
    Posts
    • #33384
      MikeS
      Participant

      I need to perform error trapping on a New form before it is saved and a SPD workflow kicks off.

      The script should check that one or more single lookup fields have been changed from the default that loads with the New form. A lookup field could be:

      1) blank when the New form loads – displays “(None)” or

      2) (if the lookup field is required) it would default to the first entry of the list to which the lookup is being performed on.

      The validation would check to see if these lookup fields had been changed from the default, and if not, require the user to fix the error before the form can be saved.

      Thanks for your help,
      MikeS

    • #33391
      Alexander Bautz
      Keymaster

      Hi,
      I would recommend setting the field as NOT required in the list settings and then use a DFFS rule to set it as required. This would ensure it always default to “(None)” on load and you can use this snippet:

      function dffs_PreSaveAction() {
          if (String(spjs.dffs.beforeProperties.LookupProject) === String(getFieldValue("LookupProject"))) {
              spjs.dffs.alert({
                  "title": "",
                  "msg": "Please select a value in the field <strong>" + spjs.dffs.fieldData.LookupProject.disp + "</strong> before trying to save this item."
              });
              return false;
          }
          return true;
      }

      If you cannot remove the required setting from the list settings you must use something like this, but please note that if your form encounters a validation error after postback to the server (for example a date filed being on the wrong format, or if you have some kind of validation set using Column validation in the list settings) this will see the now selected value as the “load-value” and require that you change it before saving:

      var lookupFieldLoadValue = getFieldValue("LookupProject");
      function dffs_PreSaveAction() {
          if (lookupFieldLoadValue === getFieldValue("LookupProject")) {
              spjs.dffs.alert({
                  "title": "",
                  "msg": "Please select a value in the field <strong>" + spjs.dffs.fieldData.LookupProject.disp + "</strong> before trying to save this item."
              });
              return false;
          }
          return true;
      }

      Please replace “LookupProject” with the internal name of your lookup field.

      Alexander

    • #33404
      MikeS
      Participant

      The first snippet above works when there is only one dffs_PreSaveAction in the Custom JS for the New form. However it pops up TWO alerts for some reason.

      If there are two or more PreSaveAction functions it only executes the last one. I need to validate three fields, plus I already have a dffs_PreSaveAction used to write a pulled forward field to a text field when saving. Is there any way to consolidate or rename these so I don’t have conflicts?

      Thanks
      MikeS

    • #33408
      Alexander Bautz
      Keymaster

      Hi,
      In JavaScript you cannot have multiple functions with the same name in a scope. This means that the last function vil override the previous definitions.

      To fix this you must merge all your pre-save-actions in one function.

      Alexander

    • #33410
      MikeS
      Participant

      How could I combine three validation fields in this code? I keep getting errors. For example, validate Project1, Project 2, and Project3 field. If any of the fields were missing the alert would call out that field. If two were missing there would be two alerts.

      function dffs_PreSaveAction() {
          if (String(spjs.dffs.beforeProperties.LookupProject) === String(getFieldValue("LookupProject"))) {
              spjs.dffs.alert({
                  "title": "",
                  "msg": "Please select a value in the field " + spjs.dffs.fieldData.LookupProject.disp + " before trying to save this item."
              });
              return false;
          }
          return true;
      }

      Thanks
      MikeS

      • This reply was modified 1 month, 1 week ago by MikeS.
      • #33413
        Alexander Bautz
        Keymaster

        Try something like this:

        function dffs_PreSaveAction() {
            var missingData = [];
            if(String(spjs.dffs.beforeProperties.LookupProject) === String(getFieldValue("LookupProject"))){
                missingData.push("<li>" + spjs.dffs.fieldData.LookupProject.disp + "</li>");
            }
            if(String(spjs.dffs.beforeProperties.Division) === String(getFieldValue("Division"))){
                missingData.push("<li>" + spjs.dffs.fieldData.Division.disp + "</li>");
            }
            if (missingData.length > 0) {
                spjs.dffs.alert({
                    "title": "Pre save validation error",
                    "msg": "Please select a value in these field(s) before trying to save this item:<br><ul>" + missingData.join("") + "</ul>"
                });
                return false;
            }
            return true;
        }

        Replace “LookupProject” and “Division” with your field names – and just repeat the “if” for the third field.

        Alexander

    • #33415
      MikeS
      Participant

      That is working .. sometimes. However I get two alerts all the time, and at other times the New form freezes with the two alerts on screen. This happens regardless of the number of fields I am validating (one or two or three fields) and no other JS in the Custom JS.

      MikeS

      • #33417
        Alexander Bautz
        Keymaster

        I fixed an error that could cause the dffs_PreSaveAction to be triggered two times in DFFS v4.4.5.18 on March 05, 2021.

        If you are using a version prior to this you must update to the latest version to fix it.

        Alexander

    • #33438
      MikeS
      Participant

      I was using a recent version. Upgraded to the very latest. The alerts seem to be working properly now. Thanks!
      MikeS

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