(Quick Reference)

3 Utility Clases - Reference Documentation

Authors: Matouš Kučera

Version: 1.0

Table of Contents

3 Utility Clases

3.1 CountrySelectorService

allowedCountryCodes() This method allows to get all allowed country codes. The general usage is for domain class constraints.

Example domain class Address.groovy

class Address {
   def countrySelectorService

String country

static constraints = { country(nullable: false, blank: false, validator: {val, obj -> def allowedCountryCodes = obj.countrySelectorService.allowedCountryCodes() if(!allowedCountryCodes.contains(val)) return "org.grails.plugins.countrySelector.country.notAllowed" }) } }

The Address class is usually an embedded class placed in groovy/src for a certain domain class. To use the countrySelectorService, you need to inject the service differently:

import grails.util.Holders

class Address { String country

static constraints = { country(nullable: false, blank: false, validator: {val, obj -> def allowedCountryCodes = obj.countrySelectorService.allowedCountryCodes() if(!allowedCountryCodes.contains(val)) return "org.grails.plugins.countrySelector.country.notAllowed" }) }

private static getCountrySelectorService() { def grailsApplication = Holders.getGrailsApplication() grailsApplication.mainContext.countrySelectorService } }