Thursday, June 28, 2012

Grails with java:Learn Basics of Grails Domain Constraints

Grails Constraints

grails are a framework developed in a groovy framework which internally uses java to build the applications very quickly. I got a chance to work on grails application. so I want to blog about the grails constraints.

As you know in any web programming language, we need to do the form level validations. which we can do it either at client level validation or server level validation
client level validations mean, the entered data cannot be sent to the server and do the validation at the browser, To do this, we have a lot of scripting languages like JavaScript or vb scripts.
For Server level validations, the entered data send to server and server checks for different validations that we define at the domain level.
we can use java or .net languages to do domain level validations. Groovy uses the Java language to do validations
Grails can define the domain level constraints

Grails can be defined the constraints validation on domain class on the following way

class Employee {
String eid
String name
String email
Integer sal
static constraints = {  eid:unique,name:blank, email:email:true

In the Employee domain class, validation constraints are defined in Groovy on Grails. groovy constraints are defined within the block containing all the required validation configuration for this domain class.

I am listing out the following domain level constraints.

List of domain validation constraints available in groovy and grails
the blank constraint is to specify the member variable in the domain can be null or not
usage is name blank: true/false

creditCard:- this is used to check the member variable is credit card
email:- email validator check is used to check correct valid email validation rules
inList :- to check the valid values between the list of values
To check for the set of valid values within the range
matches :- this used to compare with the regular expression
max :- checks for the allowed maximum value
maxSize :- this specifies the maximum size
min :- this is the minimum value that would not allow specified
minSize :- minimum value to allow
notEqual :- this checks for the value is not equal specified
nullable:- the member value is nullable or not
range:- specifies the range of values
unique :- unique constraint specifies that where the variable is duplicate or not

The above validators are predefined validators that come with Grails plugin framework, we can also define our custom validator using the Grails framework. The validators check and validate on form as a server-side validator

Related article