Skip to content

Model Object

allErrors()

allErrors() — returns array

Available in: model Category: Error Functions

Returns an array of all the errors on the object.

It does this by storing instances of models that are associations, and not checking associations of those instances because they have already been checked.

NameTypeRequiredDefaultDescription
includeAssociationsbooleannofalse
seenErrorsarrayno[runtime expression]is a private argument not meant to be used by the user, the function uses this to ensure circular dependency avoidance.
// 1. Get all errors on a model object after a failed validation
user = model("User").new(username="", password="");
user.valid();
errors = user.allErrors();
// errors ->
// [
//   { message: "Username must not be blank.", name: "", property: "username" },
//   { message: "Password must not be blank.", name: "", property: "password" }
// ]

// 2. Check for errors and iterate over them
if (user.hasErrors()) {
	for (error in user.allErrors()) {
		writeOutput(error.property & ": " & error.message);
	}
}

// 3. Include errors from associated models (e.g. a user with associated profile)
user = model("User").findOne(where="id=1", include="profile");
user.valid();
allErrors = user.allErrors(includeAssociations=true);
// allErrors contains errors from both user and its associated profile model