ad-hockery: /ad·hok'@r·ee/, n.
Gratuitous assumptions... which lead to the appearance of semi-intelligent behavior but are in fact entirely arbitrary.
[Jargon File]
A common domain class use-case is for a self-referential relationship that must not be circular (a directed acyclic graph). For example, a Person class may have a parent property that is a many-to-one relationship with another Person instance. However a given Person cannot be his own parent or ancestor.
We're using just such a relationship for trees of pages that inherit certain characteristics from their ancestors. In order to validate that our users aren't setting up circular references I implemented the following constraint:Then I just needed to register my new constraint by adding ConstrainedProperty.registerNewConstraint(AcyclicConstraint.NAME, AcyclicConstraint) to grails-app/conf/spring/resources.groovy. Using the constraint is as simple as this:
class Person {
String name
Person parent
static constraints = {
parent acyclic: true
}
}
The constraint can be mixed with others such as nullable: true. The really nice thing is that the constraint implementation doesn't reference any of my domain classes directly, meaning it can be re-used in any other domain class.
When you're using similar validation logic in multiple classes defining a constraint like this is a much better option than using a validator closure and like many things in Grails it turns out to be pretty easy to implement.
awesome! this constraint should definitely be added to the grails source, i reckon it is one that is enforced in all wacky ways in other projects. Adding a constraint looks nice and easy!
Nice work! Custom constraints in Grails are very powerful. Wonder if this could be extended to look beyond direct relationship and make sure that someone isn't say their own great great grand child or such.
5 comments:
awesome! this constraint should definitely be added to the grails source, i reckon it is one that is enforced in all wacky ways in other projects. Adding a constraint looks nice and easy!
Nice work! Custom constraints in Grails are very powerful. Wonder if this could be extended to look beyond direct relationship and make sure that someone isn't say their own great great grand child or such.
@dustmite it's already doing that
@Rob Sorry, I noticed that after posting my comment. Really great stuff, took it for a spin last night and works like a charm.
As @johnrellis said this should be included with the Grails source.
Please, as others have said, get this included in Grails core. It's a sufficiently complex validation that is easy to mess up and is also very common.
Post a Comment