2013-07-15 43 views
0

我不知道什麼是錯我的代碼,該值是正確的,但是當我保存它,得到它的錯誤Grails的拒絕值,類型不匹配

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 
    } 

有人能幫助我,發現我的錯誤,所以我的代碼得到錯誤的結果? 謝謝:)

+0

你試過了嗎:'def specialRateInstance = new SpecialRate(params)'' –

回答

0
  • fromCurrencyCurrency類型,但你會設置從params的字符串值。
  • validThru是不可解析因爲該格式是Mon JUl 15 00:00:00 ICT 2013和你期望它是dd/MM/yyyy

步驟來恢復: -

  • 關注@詹姆斯Kleeh。
  • 驗證fromCurrencyCurrency類型一旦設定的。
  • 匹配的日期格式validThru。確保validThruparams設置爲dd/MM/yyyy