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
@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.
Enjoy!
13 comments:
Thanks for showing us how to use the new validation feature!!!
Jeff, is domain-style data binding possible in clases marked @Validateable? I'm hoping the answer's yes as I've got a scenario where I'd like to take a map of strings, bind them to a non-domain/non-command object instance and then enact validation.
Great stuff!
thanks,
Chris Fraser
superflav,
No. Data binding is separate from validation. Adding data binding support to classes could be done, but isn't supported at the moment.
I am wondering where your map of data is coming from. If the data is coming as request params, then a command object would give you data binding and validation capabilities.
Jeff, I'd like to bind to collection fields within a command object like Rob's doing here: http://stateyourbizness.blogspot.com/2009/02/binding-to-collection-fields-on-command.html , but I'd like those collection fields to be richer data types than he's using.
I tried instantiating references to another command object in Grails 1.0.4, but that didn't work. I'm hoping that this issue is fixed in Grails 1.1 with http://jira.codehaus.org/browse/GRAILS-3266 , but if not I was thinking that your @Validateable solution might just do the trick.
Thoughts?
thanks,
Chris
Jeff, is this feature for *any* Java POJO, or just Groovy-based ones?
Any new posts or has this blog moved?
Nope, the blog hasn't moved. I have just been neglecting it pretty badly.
However, a post is coming soon. :)
superflav,
For data binding, of course the bindData method is available (and has been for some time)
Greetings Jeff Brown and readers!
I have been researching on the net in order to find some affairs which are surely going to help me n my way to finish a High School proyect. Anyway, What I meant is that this column made my day.
I'd like to get a map of sequences, bind them to a non-domain/non-command objected example and then enact validation.
help with thesis
This listing is certainly very beneficial.This is the right weblog. it's far very interesting weblog Ever. Thanks for giving one of these brilliant article.
from
for more information please check the site
oracle cloud fusion financials “>
It is nice blog.Very interesting blog this is.
oracle fusion SCM on line training
Hi,
You have posted Very good information to all readers .surely i am very much interested in reading this type of articles and develop my skills .keep posting more articles.
thank you.
oracle EBS training
Post a Comment