def b = new Book(title: "The Shining")
if (!b.validate()) {
b.errors.allErrors.each {
println it
}
}
validate
Purpose
Validates a domain class against the applied constraints (see validation)
Description
The validate method validates a domain class based on its defined Constraints. The errors are stored in the errors property of the domain class instance.
The validate method accepts an optional List argument which contains the names of the properties to be validated. When a List of names is specified, only those properties will be validated.
For Hibernate-backed domain classes, each validation starts with a fresh errors object that preserves existing data-binding failures. Previously added global errors (errors.reject(…)) and ordinary field errors (errors.rejectValue(…)) are cleared before validation runs. Constraints and beforeValidate callbacks can add errors for the current validation. This also applies when validation is triggered by save() or by flushing a dirty instance, and when deepValidate: false disables association validation.
Inspect manually added errors before invoking validation if they should prevent an operation. Place rules that must be checked on every validation in a constraint or a beforeValidate callback. Other GORM datastores can have different error-reset behavior.
Examples
def a = new Album(artist: "Genesis", title: "Nursery Cryme", releaseDate: 1971)
// only validate title and releaseDate
if (!a.validate(["title", "releaseDate"])) {
a.errors.allErrors.each {
println it
}
}
Parameters:
-
deepValidate(optional) - Whether associations of the domain instance should also be validated, i.e. whether validation cascades. This istrueby default; set it tofalseto disable cascading validation.