Selecting multiple values from select tag - Grails

2019-02-15 03:45发布

问题:

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()

回答1:

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/>.



回答2:

According to this docs: http://www.grails.org/doc/1.3.7/ref/Tags/select.html your select tag is wrong, it should be rather:

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