"Fossies" - the Fresh Open Source Software Archive

Member "grails-core-5.2.4/grails-docs/src/test/resources/docs/ref/Domain Classes/errors.html" (14 Sep 2022, 61907 Bytes) of package /linux/www/grails-core-5.2.4.tar.gz:


Caution: In this restricted "Fossies" environment the current HTML page may not be correctly presentated and may have some non-functional links. You can here alternatively try to browse the pure source code or just view or download the uninterpreted raw source code. If the rendering is insufficient you may try to find and view the page on the grails-core-5.2.4.tar.gz project site itself.

(Quick Reference)

errors

Purpose

An instance of the Spring Errors interface containing data binding and/or validation errors.

Examples

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 data binding to store type conversion errors and during validation when calling the validate or save methods.

You can also add your own errors using the reject and rejectValue methods:

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.