////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////


== errors



=== Purpose


An instance of the Spring {springapi}org/springframework/validation/Errors.html[Errors] interface containing data binding and/or validation errors.


=== Examples


[source,groovy]
----
def user = new User(params)

if (user.validate()) {
    // do something with user
}
else {
    user.errors.allErrors.each {
        println it
    }
}
----


=== Description


The `errors` property is used by Grails during link:{guidePath}theWebLayer.html#dataBinding[data binding] to store type conversion errors and during link:{guidePath}validation.html[validation] when calling the link:validate.html[validate] or link:save.html[save] methods.

You can also add your own errors using the {springapi}org/springframework/validation/Errors.html#reject(java/lang/String)[reject] and {springapi}org/springframework/validation/Errors.html#rejectValue(java/lang/String,%20java/lang/String)[rejectValue] methods:

[source,groovy]
----
if (params.password != params.confirm_password) {

    user.errors.reject(
        'user.password.doesnotmatch',
        ['password', 'class User'] as Object[],
        '[Property [{0}] of class [{1}] does not match confirmation]')

    // The following helps with field highlighting in your view
    user.errors.rejectValue(
        'password',
        'user.password.doesnotmatch')

    render(view: 'signup', model: [user: user])
}
----

In the example of `reject` above, `'user.password.doesnotmatch'`, is the error code corresponding to a value in `grails-app/i18n/message.properties`, `\['password', 'class User'\] as Object\[\]` is a Groovy cast from a `List` to an `Object` array to match the expected signature, and `'\[Property \[{0}\] of class \[{1}\] does not match confirmation\]'` is the default mapping string.

In the `rejectValue` example, `'password'` is the field in the view to highlight using a `<g:hasErrors>` tag and `'user.password.doesnotmatch'` is the i18n error code.
