Selecting multiple values from select tag - Grails

◇◆丶佛笑我妖孽 提交于 2019-12-19 04:01:48

问题


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}" />


来源:https://stackoverflow.com/questions/8476880/selecting-multiple-values-from-select-tag-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!