Skip to content

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 a Utility choice) and PONumber (Textbox); permission to edit content types in the Connections app
  • Teaches SetFieldIsValid first, 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

  1. In the Connections app, open the invoice content type.
  2. Select PONumber. Open Behaviors > Add a Behavior > Custom Behavior. Name it PO number rules, paste the script below, and select Save & Close.
  3. Add the same script to InvoiceAmount and VendorType. The rule reads all three fields, and a behavior runs only when its own field changes.

Script

Custom Behavior on PONumber, InvoiceAmount and VendorType
// 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" > 500 works only by accident, so the amount goes through parseFloat first.
  • All rules for PONumber live 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 SetFieldIsValid with 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 was InvoiceAmount.

Test it

  1. In the editor, select Test. Type 750 in InvoiceAmount. PONumber turns red with the "required" message.
  2. Select Utility in VendorType. The message clears.
  3. Select another type, then type 12345 in PONumber. The format message shows. Type PO-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 "" to SetFieldValidationMessage. Never do that. Clear messages with SetFieldIsValid(field, true).
  • The message never shows. SetFieldIsValid runs after SetFieldValidationMessage in 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.