Part Validation Examples
Part validation allows you to enforce rules on the values entered by the user. Validation is configured in the Advanced tab of the part using:
Set validation error via — selects the validation method
Validation error — contains the rule and the message shown when the rule fails
You can reference the value of any part using:
#REPLACE-PARTVALUE-<InternalName>||#Validation Methods Overview
Method
Capabilities
Typical Use Cases
Plain text
Valid when the field contains at least one character
Simple “field cannot be empty” checks
NCalc
Expression-based logic, string comparison, date comparison, math
Dates, numeric logic, conditional checks
JavaScript
Full scripting engine with conditional logic and custom error messages
Complex rules, multi-part validation, document template validation
Validation Examples
Below are common validation scenarios with examples for date logic, comparisons, and required fields.
1. Compare two fields (must be equal)
Scenario: Part FieldTwo must match the value of FieldOne.
Set validation error via: JavaScript
Validation error:
if (#REPLACE-PARTVALUE-FieldOne||# !== #REPLACE-PARTVALUE-FieldTwo||#)
return 'Field one must be equal to field two';2. Compare dates (one date must be after another)
Scenario:
DateToCompareTo contains a reference date (possibly filled via a workflow).
DateAfterDateToCompareWith must be a later date.
Default value for today (reference date)
Default value type: NCalc Default value:
FORMAT('{0:yyyyMMdd}', NOW())Validation rule
Set validation error via: JavaScript Validation error:
if (#REPLACE-PARTVALUE-DateAfterDateToCompareWith||# < #REPLACE-PARTVALUE-DateToCompareTo||#)
return 'Date must be after the comparison date';3. Date must be in the future
Scenario: The entered date must be today or later.
Set validation error via: NCalc Validation error:
IIF(
'#REPLACE-PARTVALUE-DateAfterToday||#' < FORMAT('{0:yyyyMMdd}', NOW()),
'Date must be after today',
''
)4. Number must not be negative
Scenario: The part value must be zero or higher.
Set validation error via: JavaScript Validation error:
var thenumber = Number(#REPLACE-PARTVALUE-nummer||#);
if (thenumber && thenumber < 0)
return "Only positive numbers allowed";Validation for Document Templates
Document Templates often require validation across multiple document parts. Using JavaScript validation allows you to check multiple fields and return detailed error messages.
Validation scripts for documents return an object:
{
success: <true/false>,
errormessage: "<message>",
errormessages: {
<InternalName>: "<message>",
...
}
}5. Make two document fields required
Fields:
Doc_CategoryDoc_Versie
Set validation error via: JavaScript Validation error:
var toreturn = { success: true };
var parts = JSON.parse(g_data_string).parts;
var category = parts.filter(p => p.internalname === "Doc_Category").map(p => p.value).join("");
var version = parts.filter(p => p.internalname === "Doc_Versie").map(p => p.value).join("");
if (!category || !version) {
toreturn.success = false;
toreturn.errormessages = {};
if (!category && !version) {
toreturn.errormessage = "'Category' and 'Version' are required";
} else if (!category) {
toreturn.errormessage = "'Category' is required";
} else if (!version) {
toreturn.errormessage = "'Version' is required";
}
if (!category) {
toreturn.errormessages.Doc_Category = "A value is required";
}
if (!version) {
toreturn.errormessages.Doc_Versie = "A value is required";
}
}
return toreturn;6. Make three document fields required
Fields:
Doc_CategoryDoc_VersionDoc_Version_date
Set validation error via: JavaScript Validation error:
var toreturn = { success: true };
var parts = JSON.parse(g_data_string).parts;
var category = parts.filter(p => p.internalname === "Doc_Category").map(p => p.value).join("");
var version = parts.filter(p => p.internalname === "Doc_Version").map(p => p.value).join("");
var versiondate = parts.filter(p => p.internalname === "Doc_Version_date").map(p => p.value).join("");
if (!category || !version || !versiondate) {
toreturn.success = false;
toreturn.errormessages = {};
var missingfields = [];
if (!category) {
missingfields.push("Category");
toreturn.errormessages.Doc_Category = "A value is required";
}
if (!version) {
missingfields.push("Version number");
toreturn.errormessages.Doc_Version = "A value is required";
}
if (!versiondate) {
missingfields.push("Version date");
toreturn.errormessages.Doc_Version_date = "A value is required";
}
toreturn.errormessage =
"One or more fields are required: " +
missingfields.map(fld => "'" + fld + "'").join(", ");
}
return toreturn;Summary
This chapter provides clear examples for:
field-to-field comparisons
date validation
numeric validation
multi-field validation for document templates
differences between Plain text, NCalc, and JavaScript validation
By applying these patterns, you can enforce simple and advanced validation logic consistently across your templates.
Last updated
Was this helpful?