Model Object
hasErrors()
Signature
Section titled “Signature”hasErrors() — returns boolean
Available in: model
Category: Error Functions
Description
Section titled “Description”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.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
property | string | no | — | Name of the property to check if there are any errors set on. |
name | string | no | — | Error name to check if there are any errors set with. |
Examples
Section titled “Examples”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.");
}