Protiviti / SharePoint Blog

SharePoint Blog

November 10
Conditionally Validate Attachments in Nintex Forms Using JavaScript

When using Nintex Forms and Workflow together in a status driven process we sometimes find ourselves needed unique validation based on a status. While Nintex provides the ability to do conditional validation logic for most controls this is not the case for attachments. Nintex provides some very useful functionality to validate the attachments control, such as minimum and maximum attachment limits as well as file format validation. However, there is no ability to conditionally validate attachments, or write custom JavaScript validation for the attachment control itself.  In order to validate the attachments you will need to add JavaScript validation to another control, but don’t worry it’s easy.

Let’s take a simple form that is a Title, Attachments, and Status control configured as a dropdown. (As the workflow will be changing the status, we will be hiding this control later, which is why there are no labels on it)

As we cannot run the validation directly on the Attachments control, we will be running it on Status control, which we will need to configure. 

1. Enter ‘val-status’ in the “CSS class” box
     - We will be using this to get the current status
2. Select ‘Yes’ for “Use custom validation”
3. Enter the name of the validation function, ours will be titled ‘validateAttachments’, in the “Custom validation function” box
4. Enter a message in the “Custom error message” box if you would like

Next you will need to add in the custom validation function to the Custom JavaScript section of the Form settings: 

The final step is to add a formatting rule on the Status control so it is always hidden. To add a rule, select the control and click ‘Add Rule’ from the ribbon, then create a rule to always hide the control. I used the always-true statement 1==1.

Note: If you set the control visibility to ‘No’ via control settings the validation will not run on the control, you must hide the control via a rule.

Now when you preview the form and try to save without adding an attachment you will get an error and the Attachment box will be highlighted.

That’s all you have to do, you can modify this code to work with any dropdown field you would like. Additionally, it is not mandatory to hide the Status field, as you may want to run the validation based on a user-selected value, just be aware that the control that calls the validation function (in this case Status) will also be highlighted in red if it is invalid. Below I’ll break down the code a little bit more to help you understand what is going on. 

Quick Launch

© Protiviti 2021. All rights reserved.   |   Privacy Policy