Grails拒绝值,类型不匹配
我不知道我的代码出了什么问题,值是正确的,但当我保存它时,它会出错Grails拒绝值,类型不匹配,grails,grails-2.0,grails-domain-class,grails-controller,Grails,Grails 2.0,Grails Domain Class,Grails Controller,我不知道我的代码出了什么问题,值是正确的,但当我保存它时,它会出错 grails.validation.ValidationException: Validation Error(s) occured during save(): - Field error in object 'specialRate' on field 'fromCurrency': rejected value [IDR - Indonesian Rupiah]; codes [typeMismatch.specialRa
grails.validation.ValidationException: Validation Error(s) occured during save():
- Field error in object 'specialRate' on field 'fromCurrency': rejected value [IDR - Indonesian Rupiah]; codes [typeMismatch.specialRate.fromCurrency, ......
grails.validation.ValidationException: Validation Error(s) occured during save():
- Field error in object 'specialRate' on field 'validThru': rejected value [Mon JUl 15 00:00:00 ICT 2013]; codes [typeMismatch.specialRate.fromCurrency, ...... could not parse date: Unparsable date: "15/07/2013"]
这是我的域名类
import java.util.Date;
import Currency;
class SpecialRate {
static auditable = true
String bookingCode
Currency fromCurrency
Date validThru
static constraints = {
bookingCode(blank: false, maxSize :20)
fromCurrency(blank:true, nullable: true)
validThru(blank: true, nullable: true)
}
}
下面是保存控制器:
def save = {
def specialRateInstance = new SpecialRate()
specialRateInstance.properties = params
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy")
specialRateInstance.validThru = formatter.parse(params.validThru)
def fromCurrency = Currency.get(params.fromCurrency);
specialRateInstance.fromCurrency = fromCurrency
withFormat {
html {
withForm {
try {
specialRateInstance = specialRateService.save(specialRateInstance)
}
catch (grails.validation.ValidationException e) {
logger.error("Missing property or validation fails", e)
}
catch (RuntimeException e) {
logger.error(e.getMessage(), e)
redirect(controller: "error", action: "serverError")
return
}
if (!specialRateInstance.hasErrors()) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'specialRate.label', default: 'SpecialRate'), specialRateInstance.bookingCode])}"
redirect(action: "show", id: specialRateInstance.id)
}
else {
render(view: "create", model: [specialRateInstance: specialRateInstance ])
}
}.invalidToken {
redirect(controller: "error", action: "forbidden")
}
}
}
}
以及服务:
def save (def specialRateInstance){
specialRateInstance.save(failOnError: true)
return specialRateInstance
}
任何人都可以帮我找到我的错误,这样我的代码就能得到错误结果吗?
谢谢:)
的类型为fromCurrency
,但您需要从Currency
设置字符串值李>params
是不可解析的,因为格式是validThru
,您希望它是Mon JUl 15 00:00:00 ICT 2013
dd/MM/yyyy
- 跟随@jameskleeh
- 一旦设置,验证
是否为from Currency
类型Currency
- 匹配
的日期格式。确保validThru
在validThru
中设置为params
李>dd/MM/yyyy
def specialRateInstance=new SpecialRate(params)