Back
afterValidationOnCreate()
Registers one or more callback methods that should be executed after a new object has been validated (i.e., when running validations during a create() or save() on a new record). This hook is useful when you want to apply custom logic only during new record creation, not during updates.
1. Add a creation-only error
afterValidationOnCreate("checkSignupEmail");
function checkSignupEmail() {
if (listFindNoCase("banned@example.com,blocked@example.com", this.email)) {
this.addError("email", "This email address cannot be used for registration.");
}
}
2. Generate a default username if missing
afterValidationOnCreate("generateUsername");
function generateUsername() {
if (!len(this.username)) {
this.username = listFirst(this.email, "@");
}
}
3. Multiple callbacks
afterValidationOnCreate("checkSignupEmail,generateUsername");
4. Example in User.cfc
component extends="Model" {
function config() {
validatesPresenceOf("email");
validatesFormatOf(property="email", regex="^[\w\.-]+@[\w\.-]+\.\w+$");
afterValidationOnCreate("checkSignupEmail,generateUsername");
}
function checkSignupEmail() {
if (listFindNoCase("banned@example.com", this.email)) {
this.addError("email", "This email address is restricted.");
}
}
function generateUsername() {
if (!len(this.username)) {
this.username = listFirst(this.email, "@");
}
}
}
Copied!