Learn Java for Web Development

(Tina Meador) #1

324 CHAPTER 7: Rapid Web Development with Grails


Listing 7-9. Book Domain Class


1.package bookstore
2.
3.class Book {
4.String bookTitle
5.Long price
6.Long isbn
7.
8.static constraints = {
9.bookTitle(blank:false)
10.price(blank:false)
11.}
12.String toString() {
13.bookTitle
14.}
15.}


Constraints in lines 8 to 11 in Listing 7-9 provide Grails with a declarative mechanism for defining
validation rules. Table 7-2 illustrates the constraints available with Grails.


Table 7-2. Constraints Available with Grails


Constraint Description
blank Validates that a String value is not blank
creditCard Validates that a String value is a valid credit card number
email Validates that a String value is a valid e-mail address
inList Validates that a value is within a range or collection of constrained values
matches Validates that a String value matches a given regular expression
max Validates that a value does not exceed the given maximum value
maxSize Validates that a value’s size does not exceed the given maximum value
min Validates that a value does not fall below the given minimum value
minSize Validates that a value’s size does not fall below the given minimum value
notEqual Validates that a property is not equal to the specified value
nullable Allows a property to be set to null; defaults to false
range Uses a Groovy range to ensure that a property’s value occurs within a specified range
scale Sets the desired scale for floating-point numbers (i.e., the number of digits to the right of the
decimal point)
size Uses a Groovy range to restrict the size of a collection or number or the length of a String
unique Constrains a property as unique at the database level
url Validates that a String value is a valid URL
validator Adds custom validation to a field
Free download pdf