Back
validateOnCreate()
Registers one or more validation methods that will be executed only when a new object is being inserted into the database. This is useful for rules that should apply strictly at creation and not during updates. You can also control whether the validation runs using the condition and unless arguments.
1. Validate new objects before insertion
function config() {
validateOnCreate("checkPhoneNumber");
}
function checkPhoneNumber() {
// Ensure area code is '614'
return Left(this.phoneNumber, 3) == "614";
}
2. Register multiple methods for validation on creation
function config() {
validateOnCreate("checkPhoneNumber, checkEmailFormat");
}
function checkEmailFormat() {
// Ensure email contains '@'
return Find("@", this.email);
}
3. Conditional validation using `condition`
function config() {
// Only validate phone number if the country is US
validateOnCreate("checkPhoneNumber", condition="this.country == 'US'");
}
4. Skip validation under certain conditions using `unless`
function config() {
// Skip phone number validation if user is a guest
validateOnCreate("checkPhoneNumber", unless="this.isGuest");
}
Copied!