Artikel Terbaru

Thursday, 28 June 2012

Grails with java:Learn Basics of Grails Domain Constraints

Grails Constraints

grails is framework developed in groovy framework which internally uses java to build the applications very quickly. I got 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 means, the entered data can not be send to server an d do the validation at the browser, To do this, we have 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 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 with in block containing all the required validation configuration for this domain class.

i am listing out the following domain level constraints.


List of domain validation constrains available in groovy and grails
blank:-
blank constraint is to specify the member variable in 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 with in range
matches :- this used to compare with the regular expression
max :- checks for allowed maximum value
maxSize :- this specifies the maximum size
min :- this is 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 null able 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 grails framework. The validators checks and validates on form as server side validator