Levy
Scout

How to run a validation function on a Save Button and have the ability to abort saving?

From the documentation there is no description on how to validate on the save button click with custom JavaScript and have the ability to abort saving.

Does anybody know how to do so?

Labels: (1)
0 Kudos
Reply
8 Replies
MegaJerk
Collaborator

Re: How to run a validation function on a Save Button and have the ability to abort saving?

like this? 

https://youtu.be/Ea6w14i382g

 

The only other thing I can think of is that you have it set as Save and Continue which may not be running Validation Rules as those typically only run with *Submitting* the form: https://help.nintex.com/en-US/office365/Designer/FormControls/ActionPanel.htm

 

Learn how use Submit Rules to build custom validation in Nintex Forms for Office 365. Want to know more about using Nintex in your organization? Head to http...
0 Kudos
Reply
Levy
Scout

Re: How to run a validation function on a Save Button and have the ability to abort saving?

Hi MegaJerk,

 

I am on classic designer, sorry, forgot to mentioned it.

It is the Save Button not any other.

0 Kudos
Reply
MegaJerk
Collaborator

Re: How to run a validation function on a Save Button and have the ability to abort saving?

Alright then.

 

On your form, Double Click on your Save Button:

MegaJerk_0-1630071094511.png

 

Inside of the Control Settings, open the Advanced Menu and make sure that Causes Validation is set to "Yes" as shown: 

MegaJerk_1-1630071389822.png

 

This should make the form Validate whenever you click on the button!

 

Note: In a previous forum post you mentioned using your own Validation that was NOT part of the built in Validation Rule System. It is important to realize that this ONLY forced Validation using the Built-In Rule System that is in place on the form! If you are using your own validation methods, then you'll need to make sure that they are either tied into regular Nintex Validation system or are triggered via the Client Click option there on the Settings:

MegaJerk_2-1630071712596.png

 

With a formula like: 

(function(event) {  
  if (myCustomValidationFunction() === "invalid") {
    // Scroll to the top to display invalidation message
    NWF$("#s4-workspace").scrollTop(0);
    // STOP the form from submitting!
    event.preventDefault();
  } else {
    // Let The Form Save
  }
}(event));

 

(note: replace "myCustomValidationFunction()" with whatever your actual function name is, and make sure that the equality test is setup correctly!)

 

That should set you straight in any scenario

 

Reply
Levy
Scout

Re: How to run a validation function on a Save Button and have the ability to abort saving?

Hi Megajerk,

 

i tried your suggested solution but I got validateAttachments is not defined.

No matter if I had function validateAttachments () {} or validateAttachments = function() {} in my JavaScript-file.

When I add my validation code directly into the fileld it is working fine:

 

(function (event) {
    var rowCount = NWF$("table[id^='idAttachmentsTable'] tr").length;
    
    if (rowCount === 0) {
        alert("Bitte fügen Sie dem Antrag ein Dokument hinzu!");
        event.preventDefault();
    }
}
(event));

 

Thank you very much, best regards, Ronny

0 Kudos
Reply
Levy
Scout

Re: How to run a validation function on a Save Button and have the ability to abort saving?

Suddenly it stopped working without any changes.

After I confirm the alert box which was called from my validation code, a Nintex Saving... Dialog with a spinner comes up and never stops.

 

Some sort of timing issue because of event.preventDefault() ?

 

0 Kudos
Reply
Levy
Scout

Re: How to run a validation function on a Save Button and have the ability to abort saving?

New Finding:

My validation code is only working if any other control is invalid. (So, not suddenly)

If all other Controls are valid, the spinner never stops.

 

0 Kudos
Reply
MegaJerk
Collaborator

Re: How to run a validation function on a Save Button and have the ability to abort saving?

without seeing your validation function, what it returns or how it's called, it's really difficult to say what the issue is. That's the issue with custom JavaScript, you get all of the customization in the world, but an update or errant typo can make your form not work.

 

my first suggestion would be to make sure that your validation code works by looking at the Dev Console while you are submitting to see if any errors are being thrown.

 

my other suggestion would be to move your validation from whatever internal code you have written and out into the Validation Rule system where things like this can run on any given control independently

0 Kudos
Reply
Levy
Scout

Re: How to run a validation function on a Save Button and have the ability to abort saving?

Hi megajerk,

thanks for coming back.

As I previously wrote, I have added the validation code directly to the client click field to the save button:

(function (event) {
    var rowCount = NWF$("table[id^='idAttachmentsTable'] tr").length;
    
    if (rowCount === 0) {
        alert("Bitte fügen Sie dem Antrag ein Dokument hinzu!");
        event.preventDefault();
    }
}
(event));

 

I cannot bring this sort of validation to the Validation Rule System because, there is no Attachment-Control for the related list item in this task form.

 

0 Kudos
Reply