Skip to content

Model Configuration

validateOnCreate()

validateOnCreate() — returns void

Available in: model Category: Validation Functions

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

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).
// 1. Register a custom method to validate new objects before insert
function config() {
	// `checkPhoneNumber` will only be called when creating a new record.
	validateOnCreate("checkPhoneNumber");
}

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

// 2. Register multiple validation methods at once
function config() {
	validateOnCreate("checkPhoneNumber,checkReferralCode");
}

function checkPhoneNumber() {
	return Left(this.phoneNumber, 3) == "614";
}

function checkReferralCode() {
	if (Len(this.referralCode) && !isValidReferral(this.referralCode)) {
		addError(property="referralCode", message="Invalid referral code.");
	}
}

// 3. Only run the validation when a condition is met
function config() {
	// Only validate the phone number on create when the user is in the US.
	validateOnCreate(methods="checkPhoneNumber", condition="this.country eq 'US'");
}

function checkPhoneNumber() {
	return IsNumeric(this.phoneNumber);
}