Validate Fields with Your Own Messages¶
An accounts payable team needs a PO number on every invoice over 500, except from utility vendors, and every PO number must follow the house format. The indexer should see exactly what is wrong, on the field they must fix, and be stopped from submitting until it is fixed. This page shows the validation pattern for one field and across fields.
At a glance
- Extension point Custom Index Behavior
- You need
InvoiceAmount(Textbox or Currency),VendorType(DropDownList with aUtilitychoice) andPONumber(Textbox); permission to edit content types in the Connections app - Teaches
SetFieldIsValidfirst, message second; one script per validated field; attaching a rule to every field it reads
Try the built-in behaviors first
Required, Input Mask, Length, Number and Date range validations, and the Alternate Requirement Behavior ("One of these fields requires a value.") need no code. Use a script when the rule depends on other fields or you need your own message.
Set it up¶
- In the Connections app, open the invoice content type.
- Select
PONumber. Open Behaviors > Add a Behavior > Custom Behavior. Name itPO number rules, paste the script below, and select Save & Close. - Add the same script to
InvoiceAmountandVendorType. The rule reads all three fields, and a behavior runs only when its own field changes.
Script¶
// Attach to: PONumber, InvoiceAmount, VendorType (same script on each)
// Reads: InvoiceAmount, VendorType, PONumber. Writes: validity and message of PONumber.
var amount = parseFloat((panel["InvoiceAmount"].RuntimeValue || "").toString().replace(/[$,\s]/g, "")) || 0;
var vendorType = (panel["VendorType"].RuntimeValue || "").toString();
var poNumber = (panel["PONumber"].RuntimeValue || "").toString().trim();
var message = "";
if (amount > 500 && vendorType !== "Utility" && poNumber === "") {
message = "A PO number is required for invoices over 500.";
} else if (poNumber !== "" && !/^PO-\d{6}$/.test(poNumber)) {
message = "Use the form PO-123456.";
}
// Validity first. true also clears old messages on the field.
builder.SetFieldIsValid("PONumber", message === "");
if (message !== "") {
builder.SetFieldValidationMessage("PONumber", message);
}
How it works¶
- Every input is read as a string and converted.
"750" > 500works only by accident, so the amount goes throughparseFloatfirst. - All rules for
PONumberlive in one script. All Custom Behaviors share one valid flag per field, so two scripts on the same field would overwrite each other. - The script decides one message, or none. It calls
SetFieldIsValidwith the result, then adds the message only when there is one. SetFieldIsValid("PONumber", true)also clears the old message, so the field turns clean as soon as the value is fixed.- The message goes on
PONumber, the field the user must fix, even when the trigger wasInvoiceAmount.
Test it¶
- In the editor, select Test. Type
750inInvoiceAmount.PONumberturns red with the "required" message. - Select
UtilityinVendorType. The message clears. - Select another type, then type
12345inPONumber. The format message shows. TypePO-123456. The message clears.
You are done when the Index app blocks submit while PONumber is red, and allows it once the value is fixed.
What can go wrong¶
Watch out
- "Could not find localized string." under the field. The script passed
""toSetFieldValidationMessage. Never do that. Clear messages withSetFieldIsValid(field, true). - The message never shows.
SetFieldIsValidruns afterSetFieldValidationMessagein the same run, which clears the message. Call it first. - The message never clears. The happy path does not call
SetFieldIsValid(field, true). - The message does not update when one field changes. The script is not attached to that field.
- The message splits into two lines. It contains
;. The panel splits messages there. - Server runs send the work item to a person. On the server an invalid field stops the document from being marked as indexed. In Start QC Task the work item then gets a QC task, even with straight-through processing on. That is usually what you want.
- The rule must hold for documents that never open in a panel. Behaviors run on the server only in Index Your Documents, Run Indexing Behaviors, Start QC Task and Search bulk edit. Enforce the rule in the workflow too if documents can skip all of them.
Variations¶
A limit that depends on a choice. Keep the limits in a map and put the values in the message:
var maxDiscount = { "Standard": 5, "Preferred": 10, "Wholesale": 25 };
var allowed = maxDiscount[(panel["CustomerType"].RuntimeValue || "").toString()] || 0;
var discount = parseFloat((panel["DiscountPercent"].RuntimeValue || "").toString()) || 0;
Then validate DiscountPercent with discount >= 0 && discount <= allowed, as in the script above.
Related¶
- Date Validation Rules
- Format and Normalize Field Values: format and validate a single field.
- Custom Index Behavior Reference
- Debugging Custom Index Behaviors