Grails - Validation



Description

Grails validation functionality uses Spring’s Validator API for data binding and validation efficiency. Grails uses this package to specify the validation constraints within constraints system.

Consider the below code for declaring constraints in a domain class with constraints property.

class UserInfo {
    String fname
    String lname
    Integer age

    static constraints = {
      ...
    }
}

The method calls can be used to that match property name to specify the constraints as shown below:

class UserInfo {
    ...

    static constraints = {
      fname size: 5..10, blank:false   //it specifies that first name should be 5 and 10 characters long
      lname size: 5..10, blank:false
      age min:20                       //it specifies age should be minimum of 20
    }
}

Grails provides different types of validation concepts as shown in the table below:

S.N.Type & Description
1 Validating Constraints
The constraints can be validated by using the validate() method.
2 Validation on the Client
It display the errors, if you get errors on validating the client side.
3 Validation and Internationalization
Grails displays the error messages by using the FieldError class from message bundles.
Advertisements