【问题标题】:Grails rejected value, type mismatchGrails拒绝值,类型不匹配
【发布时间】:2013-07-15 06:59:00
【问题描述】:

我不知道我的代码有什么问题,值是正确的,但是当我保存它时,它会出错

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
    }

任何人都可以帮助我并发现我的错误,所以我的代码得到错误结果? 谢谢:)

【问题讨论】:

  • 你试过了吗:def specialRateInstance = new SpecialRate(params)

标签: grails grails-2.0 grails-domain-class grails-controller


【解决方案1】:
  • fromCurrency 的类型为 Currency,但您将设置来自 params 的字符串值。
  • validThru 是无法解析的,因为格式是 Mon JUl 15 00:00:00 ICT 2013,而您期望它是 dd/MM/yyyy

恢复步骤:-

  • 关注@James Kleeh。
  • 一旦设置,验证 fromCurrencyCurrency 类型。
  • 匹配validThru 的日期格式。确保validThruparams 中设置为dd/MM/yyyy

【讨论】:

    猜你喜欢
    • 2013-05-05
    • 2012-07-24
    • 2014-05-19
    • 2018-06-08
    • 1970-01-01
    • 2019-03-14
    • 2018-09-19
    • 2018-10-08
    相关资源
    最近更新 更多