Last updated by
3 years ago
Page: Ref Code Plugin, Version:2
Ref Code Plugin
This plugin provides an easy solution for 'code/description' reference/lookup data. Often in your business model you have simple reference/lookup tables with just code and description columns; e.g. Country (uk=United Kingdom, nl=Netherlands, ...), OrderStatus (C=Cancelled, N=New, P=Pending, S=Shipped), OrganizationType, ...In Grails it's easy to create specific domain classes and generate the controller and views, but still you end up with code you have to maintain. An alternative in Grails is to use the inList constraint, but this type of constraint only uses really fixed values.This plugin contains a simple single domain class in which you can store domains and the allowable values. The allowable values are split up in to the real value, a abbreviation, and a meaning. It also contains a taglib for easily creating HTML selects for a given domain, and a service which can be used to validate input values against the allowable values in custom constraints.Installation
To install the Ref Code plugin type this command from your project's root folder:grails install-plugin ref-code
grails generate-all RefCode
RefCode TagLib
The ref code taglib provides the following tags:select
Renders a HTML select for the given domainAttributes:domain - the domain nameExample:<refcode:select domain="orderStatus" /> <refcode:select domain="orderStatus" noSelection="['':'']" />
abbreviation
Renders the abbreviation for the given domain and valueAttributes:domain - the domain name value - the valueExample:<refcode:abbreviation domain="orderStatus" value="1" /> <refcode:abbreviation domain="orderStatus" value="${order.status}"/>
meaning
Renders the meaningfor the given domain and valueAttributes:domain - the domain name value - the valueExample:<refcode:meaning domain="orderStatus" value="1" /> <refcode:meaning domain="orderStatus" value="${order.status}"/>
Domain Class validation
To validate input data against a domain's allowable values you can use the RefCodeService in your custom validator. Example:class Order{ def refCodeService .. <other properties>
String status static constraints = {
status(validator: { val, obj -> return obj.refCodeService.validate("orderStatus", val) })
}
}Domain Class validation using refCode Constraint
To validate input data against a domain's allowable values you can use the refCode constraint (0.2 version of RefCode Plugin).class Order{ def refCodeService .. <other properties>
String status static constraints = {
status(refCode:"orderStatus")
}
}