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
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.