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 the grails application. so I want to blog about the grail constraints.
As you know in any web programming language, we need to do the form level validations. which we can do 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 the server, and the 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 as the constraints validation on domain class in 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 configurations for this domain class.
I am listing out the following domain-level constraints.
List of domain validation constraints available in groovy and grails
blank:-
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 a 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 duplicated or not
The above validators are predefined validators that come with the Grails plugin framework, we can also define our custom validator using the Grails framework. The validators check and validate on the form as a server-side validator.