Skip to content

Model Configuration

validate()

validate() — returns void

Available in: model Category: Validation Functions

Registers method(s) that should be called to validate objects before they are saved.

NameTypeRequiredDefaultDescription
methodsstringno—Method name or list of method names to call. Can also be called with the method argument.
conditionstringno—String expression to be evaluated that decides if validation will be run (if the expression returns true validation will run).
unlessstringno—String expression to be evaluated that decides if validation will be run (if the expression returns false validation will run).
whenstringnoonSavePass in onCreate or onUpdate to limit when this validation occurs (by default validation will occur on both create and update, i.e. onSave).
// 1. Register a custom validation method to run on every save
function config() {
	// `checkPhoneNumber` will be called whenever an object is created or updated.
	validate("checkPhoneNumber");
}

function checkPhoneNumber() {
	// Make sure the area code is `614`.
	return Left(this.phoneNumber, 3) == "614";
}

// 2. Register multiple custom validation methods at once
function config() {
	validate(methods="checkPhoneNumber,checkEmailDomain");
}

// 3. Limit validation to create or update only
function config() {
	// Only run `checkTrialExpiry` when updating an existing record.
	validate(methods="checkTrialExpiry", when="onUpdate");
}

// 4. Run a custom validation only when a condition is met
function config() {
	// `checkBillingAddress` is skipped when the order is free.
	validate(methods="checkBillingAddress", condition="this.totalAmount gt 0");
}

// 5. Skip a custom validation when an `unless` expression is true
function config() {
	// `checkCreditCard` is skipped for admin users.
	validate(methods="checkCreditCard", unless="this.isAdmin");
}