Fork me on GitHub

29 Jun 2010

Acyclic relationship validation in Grails

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.

11 comments:

johnrellis said...

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!

dustmite said...

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.

Rob said...

@dustmite it's already doing that

dustmite said...

@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.

Rick Jensen said...

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.

cerri bell said...

Jadwal Bola | Permainan Bola | Judi Bola | Taruhan Bola | Agen Bola | Agen Sbobet | Jasa SEO Profesional | Jasa SEO | Harga Jasa SEO Profesional | Jasa SEO Murah | Pakar SEO

octavinsu said...

338a | agen bola | agen sbobet | bola tangkas | casino sbobet | liga champion | prediksi bola | cool mobile gagdet

mobilsuv2012 said...

sbobet | ibcbet | agen bola | agen bola Terpercaya | Jasa SEO Profesional | Jasa SEO | Harga Jasa SEO Profesional | Jasa SEO Murah | Pakar SEO

Metamorfosis said...

Pelatihan SDM is network marketing and training information or training an employee who has worked with many consulting firms and training institutions.
pelatihan hrd,
pelatihan pengembangan diri,
pendidikan dan pelatihan sdm,
pelatihan akuntansi keuangan,
pelatihan sales marketing,
pelatihan kehumasan,
pelatihan sdm jogja,
lembaga pelatihan sdm
pelatihan manajemen bisnis,
pelatihan administrasi perkantoran,
lembaga training motivasi,
pelatihan sekretaris,
pelatihan karyawan,
lembaga training di jogja,
pelatihan in house training,
pelatihan public training,
jasa konsultan,
pelatihan pengembangan sdm,
pelatihan manajemen sdm,
program training karyawan,
diklat sdm,
pelatihan sdm untuk rumah sakit,
_____
id card murah,
harga id card,
cetak kartu santri,
produksi id card,
bikin id card,
cetak id card jogja,
id card murah jogja,
cetak id card,
percetakan id card,
pabrik id card,
cetak member card,
cetak kartu pelajar,
cetak kartu siswa,

ssamira Butik said...

I like your stuff, classic right, Now... everything with digital, perdita di peso thanks for remind me for this memories
water heater gainsborough
water heater ariston
water heater rinnai
water heater delizia
water heater wika

Metamorfosis said...

Kursus Arab Pare is a place for all of you who want to study and able to speak arabic. Come and join us..

Tempat Kursus Bahasa Arab