Back
afterValidationOnUpdate()
Registers one or more callback methods that should be executed after an existing object has been validated (i.e., when running validations during an update() or save() on an already-persisted record). This hook is useful when you want logic to run only on updates, not on initial creation.
1. Prevent updating restricted emails
afterValidationOnUpdate("checkRestrictedEmail");
function checkRestrictedEmail() {
if (this.email eq "admin@example.com") {
this.addError("email", "You cannot change this email address.");
}
}
2. Automatically update a lastModifiedBy field
afterValidationOnUpdate("setLastModifiedBy");
function setLastModifiedBy() {
this.lastModifiedBy = session.userId;
}
3. Multiple callbacks
afterValidationOnUpdate("checkRestrictedEmail,setLastModifiedBy");
4. Example in User.cfc
component extends="Model" {
function config() {
validatesPresenceOf("email");
validatesFormatOf(property="email", regex="^[\w\.-]+@[\w\.-]+\.\w+$");
afterValidationOnUpdate("checkRestrictedEmail,setLastModifiedBy");
}
function checkRestrictedEmail() {
if (this.email eq "admin@example.com") {
this.addError("email", "This email cannot be changed.");
}
}
function setLastModifiedBy() {
this.lastModifiedBy = session.userId;
}
}
Copied!