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

  • 26 August 2021
  • 8 replies
  • 602 views

Badge +6

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?


8 replies

Userlevel 5
Badge +14

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


 

Badge +6

Hi MegaJerk,


 


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


It is the Save Button not any other.

Userlevel 5
Badge +14

Alright then.


 


On your form, Double Click on your Save Button:



 


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



 


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:



 


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


 

Badge +6

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

Badge +6

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() ?


 

Badge +6

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.


 

Userlevel 5
Badge +14

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

Badge +6

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.


 

Reply