如何解决为带有多选枚举字段的域对象创建表单失败,并显示“属性xxx类型不匹配”
我正在使用带有GORM 7.0.7的Grails 4.0.4.RELEASE 我正在使用的数据库是MongoDB
我可以成功运行该应用程序,并导航到该域类的创建表单。我可以为单例字段someOtherEnum
选择值。我可以为categories
字段选择多个值。但是,当我提交表单时,会收到以下消息:“属性类别类型不匹配”。我根据此处发布的另一个问题的答案使用了这种方法,但它对我不起作用。我没有像该用户那样使用嵌入式枚举或hasMany
集合。
控制器和服务是由generate-all
命令生成的。
How to add a multiple enum field to the create and edit forms in Grails 4?
我在src/main/groovy/com/project/core/dsl
中有一个枚举,我正在用来为域类填充一个多选字段:
package com.project.core.dsl
import groovy.transform.CompileStatic
@CompileStatic
enum Category {
CATEGORY_ONE("Category One"),CATEGORY_TWO("Category Two"),CATEGORY_THREE("Category Three")
private final String id
private Category(String id) { this.id = id }
@Override
String toString() { id }
String getKey() { name() }
}
域类:
package com.project.core
import com.project.core.dsl.someOtherEnum
import com.project.core.dsl.Category
class DomainObject {
SomeOtherEnum someOtherEnum
Set<Category> categories
static constraints = {
}
static mapping = {
someOtherEnum index: true
}
@Override
String toString() {
return "DomainObject{" +
"someOtherEnum=" + someOtherEnum +
",categories=" + categories +
'}';
}
}
在create.gsp
视图中,我具有以下形式:
<g:form resource="${this.domainObject}" method="POST">
<fieldset class="form">
<f:with bean="domainObject">
<f:field property="someOtherEnum"/>
<f:field property="categories">
<g:select
multiple="true"
name="${property}"
from="${Category}"
value="${domainObject?.categories}"
/>
</f:field>
</f:with>
</fieldset>
<fieldset class="buttons">
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label',default: 'Create')}" />
</fieldset>
</g:form>
域类控制器:
package com.project.core
import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.*
class DomainObject {
DomainObjectService domainObjectService
static allowedMethods = [save: "POST",update: "PUT",delete: "DELETE"]
def index(Integer max) {
params.max = Math.min(max ?: 10,100)
respond domainObjectService.list(params),model:[domainObjectCount: domainObjectService.count()]
}
def show(Long id) {
respond domainObjectService.get(id)
}
def create() {
respond new DomainObject(params)
}
def save(DomainObject domainObject) {
if (domainObject == null) {
notFound()
return
}
try {
domainObjectService.save(domainObject)
} catch (ValidationException e) {
respond domainObject.errors,view:'create'
return
}
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message',args: [message(code: 'domainObject.label',default: 'DomainObject'),domainObject.id])
redirect domainObject
}
'*' { respond domainObject,[status: CREATED] }
}
}
def edit(Long id) {
respond domainObjectService.get(id)
}
def update(DomainObject domainObject) {
if (domainObject == null) {
notFound()
return
}
try {
domainObjectService.save(domainObject)
} catch (ValidationException e) {
respond domainObject.errors,view:'edit'
return
}
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.updated.message',domainObject.id])
redirect domainObject
}
'*'{ respond domainObject,[status: OK] }
}
}
def delete(Long id) {
if (id == null) {
notFound()
return
}
domainObjectService.delete(id)
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.deleted.message',id])
redirect action:"index",method:"GET"
}
'*'{ render status: NO_CONTENT }
}
}
protected void notFound() {
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.not.found.message',params.id])
redirect action: "index",method: "GET"
}
'*'{ render status: NOT_FOUND }
}
}
}
域对象服务:
package com.project.core
import grails.gorm.services.Service
@Service(DomainObject)
interface DomainObjectService {
DomainObject get(Serializable id)
List<DomainObject> list(Map args)
Long count()
void delete(Serializable id)
DomainObject save(DomainObject domainObject)
}
解决方法
通过进行以下更改,我克服了我的错误。我在DomainObject
static hasMany = [categories: Category]
我在create.gsp
文件中做了以下更改:
<f:field property="categories">
<g:select
multiple="true"
name="${property}"
from="${Category?.values()}"
optionKey="key"
value="${domainObject?.categories}"
/>
</f:field>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。