Skip to content

Model Object

hasErrors()

hasErrors() — returns boolean

Available in: model Category: Error Functions

Checks whether a model object has any validation or other errors. It returns true if the object contains errors, or if a specific property or named error is provided, it checks only that subset. This is useful for validating objects before saving them to the database or displaying error messages to the user.

NameTypeRequiredDefaultDescription
propertystringnoName of the property to check if there are any errors set on.
namestringnoError name to check if there are any errors set with.
1. Get a post object
post = model("post").findByKey(params.postId);

// Check if the object has any errors
if (post.hasErrors()) {
    writeOutput("There are errors. Redirecting to the edit form...");
    redirectTo(action="edit", id=post.id);
}

2. Check if a specific property has errors
if (post.hasErrors(property="title")) {
    writeOutput("The title field contains errors.");
}

3. Check if a specific named error exists
if (post.hasErrors(name="requiredTitle")) {
    writeOutput("The post is missing a required title.");
}