Monday, February 21, 2011

Griffon Validation Plugin 0.6 Released

Griffon Validation Plugin v0.6 was released today. In this release the validation plugin's artifact handling has been rewritten to be compatible withe the up-coming Griffon 0.9.2 release. Also in this release validation plugin now simulates inheritance effect for the constraints you define. Since the constraints are defined using static fields following Grails convention, no real inheritance can be implemented however now with 0.6 release you class will basically copy the parent class' constraints, and additionally you can also override the parent constraint. See the following example:


@Validatable
class ServerParameter {
@Bindable String serverName
@Bindable int port
@Bindable String displayName

def beforeValidation = {
setDisplayName "${serverName}:${port}"
}

static constraints = {
serverName(nullable: false, blank: false)
port(range: 0..65535)
displayName(nullable: false, blank: false)
}
}

@Validatable
class ProtocolSpecificServerParameter extends ServerParameter{
@Bindable String protocol

def beforeValidation = {
setDisplayName "${protocol}://${serverName}:${port}"
}

static constraints = {
protocol(blank: false, nullable: false)
}
}


In the above example, the ProtocolSpecificServerParameter will not only inherent ServerParameter's serverName and port fields but also their associated constraints. The only resitration you need to be aware of is if the parent constraint generates error for a certain condition then the overriding child constraint has to generate error as well. In other words, validation plugin does not allow error hiding by using constraint override in the child class, similar to the method exception handling during inheritance within Java.

No comments: