Selecting multiple values from select tag - Grails

supp picture supp · Dec 12, 2011 · Viewed 12.3k times · Source

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="validator.id"  multiple="multiple" optionKey="id" from="${com.project.Validator.list()}" value="${validators}" />

and this is in OperationLogContoller.groovy

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


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

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

schmolly159 picture schmolly159 · Dec 12, 2011

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('validator.id'))

The params.list() will always fetch 'validator.id' 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/>.