supp supp - 1 year ago 138
Groovy Question

Selecting multiple values from select tag - Grails

Could you please anyone tell me how to get multiple values from

<g:select multiple="multiple" ...

I have this in my create.gsp

<g:select name="" multiple="multiple" optionKey="id" from="${com.project.Validator.list()}" value="${validators}" />

and this is in OperationLogContoller.groovy

def create = {
def operationLogInstance = new OperationLog() = params
operationLogInstance.validator = Validator.get(params.validatorId)
operationLogInstance.operation = Operation.get(params.operationId)
return [operationLogInstance: operationLogInstance]

def save = {
def operationLogInstance = new OperationLog(params)
operationLogInstance.validator = Validator.get(;
if ( true))
flash.message = "${message(code: 'default.created.message', args: [message(code: 'operationLog.label', default: 'OperationLog'),])}"

If I select just one from select, it works perfectly but if I select two, I get the following exception:

groovy.lang.MissingMethodException: No signature of method: com.akent.Validator.get() is applicable for argument types: (java.lang.String, java.lang.String) values: [3, 4]
Possible solutions: get(java.lang.Object), getId(), getIp(), getAt(java.lang.String), getAll(), ident()

Answer Source

Your MME is because the get() on Domain classes only handles one id at a time. For multiple ids from your <select/> use.

def validators = Validator.getAll(params.list(''))

The params.list() will always fetch '' as a List even if there's only one, which will save you from having to test for single vs multiple results from your <select/>.