Grails 1.1 beta3 Is Out! Thursday, January 29, 2009

We are closing in on the much anticipated release of Grails 1.1. Today beta 3 was released. See the release notes.

A new feature that has not been documented yet involves new validation capabilities. Up to this point, domain classes and command objects within a Grails app have supported really powerful validation capabilities. See section 7 in The User Guide for info on how that works. What is new is now you can apply those same validation capabilities to any class within a Grails app, not just domain classes and command objects. Making a class validateable involves defining a static property called "constraints" and assigning a closure, just like you would in a domain class or a command object.

Second, you have to tell Grails about your validateable class. You have 2 options for doing that. One option is to mark you class with the org.codehaus.groovy.grails.validation.Validateable annotation. Another option is to define your validateable classes in grails-app/conf/Config.groovy by assigning a value to the grails.validateable.classes property. That would look something like this...

grails.validateable.classes = [ com.mycompany.dto.SomeClass, com.mycompany.SomeOtherClass]

An annotated class might look something like this...

// src/groovy/com/companyname/SomeClass.groovy
package com.companyname

import org.codehaus.groovy.grails.validation.Validateable

class SomeClass {
Integer age
String name

static constraints = {
age range: 16..66
name blank: false, size: 5..35

Note that if you are using the annotation based approach, Grails will search all classes in the app to find all of the @Validateables. While that isn't really a performance problem, you can tune that a bit by specifying some specific packages that Grails should search. To do that, assign a value to grails.validateable.packages in grails-app/conf/Config.groovy like this...

grails.validateable.packages = ['com.companyname.dto', 'com.companyname.someotherpackage']

If the grails.validateable.packages property has a value then Grails will only look in those packages (and packages below those packages) for classes marked with @Validateable.

While this is still beta software, I think the feature will end up being delivered in the final release pretty much like it is described above. If you have any input on that or any other features in Grails. We would love to hear from you. Bring it to the mailing list or file a JIRA issue.