(Quick Reference)

Constraints Usage

Constraints provide Grails with a declarative DSL for defining validation rules, schema generation and CRUD generation meta data. An example of a set of constraints applied to a domain class are as follows:

class User {
    ...

static constraints = { login(size:5..15, blank:false, unique:true) password(size:5..15, blank:false) email(email:true, blank:false) age(min:18, nullable:false) } }

Refer to the user guide topic on Constraints for more information.

Global Constraints

You can apply constraints globally inside grails-app/conf/Config.groovy as follows:

grails.gorm.default.constraints = {
   '*'(nullable:true, size:1..20)
}

The wildcard signifies that the constraints apply to all properties. You can also define shared constraints:

grails.gorm.default.constraints = {
   myShared(nullable:true, size:1..20)
}

That can be reused in your class:

class User {
    ...

static constraints = { login(shared:"myShared") } }

Quick reference

ConstraintDescriptionExample
blankTo validate that a String value is not blanklogin(blank:false)
creditCardTo validate that a String value is a valid credit card numbercardNumber(creditCard:true)
emailTo validate that a String value is a valid email address.homeEmail(email:true)
inListTo validate that a value is within a range or collection of constrained values.name(inList:"Joe", "Fred", "Bob" )
matchesTo validate that a String value matches a given regular expression.login(matches:”a-zA-Z+”)
maxEnsures a value does not exceed the given maximum value.age(max:new Date()) price(max:999F)
maxSizeEnsures a value’s size does not exceed the given maximum value.children(maxSize:25)
minEnsures a value does not fall below the given minimum value.age(min:new Date()) price(min:0F)
minSizeEnsures a value’s size does not fall below the given minimum value.children(minSize:25)
notEqualEnsures that a property is not equal to the specified valuelogin(notEqual:”Bob”)
nullableAllows a property to be set to null - defaults to false for domain class properties.age(nullable:true)
rangeUses a Groovy range to ensure that a property’s value occurs within a specified rangeage(range:18..65)
scaleSet to the desired scale for floating point numbers (i.e., the number of digits to the right of the decimal point).salary(scale:2)
sizeUses a Groovy range to restrict the size of a collection or number or the length of a String.children(size:5..15)
uniqueConstraints a property as unique at the database levellogin(unique:true)
urlTo validate that a String value is a valid URL.homePage(url:true)
validatorAdds custom validation to a field.See documentation

Scaffolding

Some constraints affect (and only affect) the scaffolding. It's not usually good practice to include UI information in your domain, but it's a great convenience if you use Grails' scaffolding extensively.

ConstraintDescription
displayBoolean that determines whether the property is displayed in the scaffolding views or not. If true (the default), the property is displayed.
editableBoolean that determines whether the property can be edited from the scaffolding views. If false, the associated form fields are displayed in read-only mode.
formatSpecify a display format for types that accept one, such as dates. For example, 'yyyy-MM-dd'.
passwordBoolean indicating whether this is property should be displayed with a password field or not. Only works on fields that would normally be displayed with a text field.
widgetControls what widget is used to display the property. For example, 'textArea' will force the scaffolding to use a <textArea> tag.