2017-05-02 35 views
0

我有這樣的命令對象:Grails的命令對象的驗證使例外,而不是錯誤

@Validateable 

class TaCustomerBoardActionCommand { 

TaCustomerBoardAction action 

static constraints = { 
    action casecade: true 
} 
} 

,並在下面的命令對象類:

class TaCustomerBoardAction { 

TaCustomerBoard taCustomerBoard 
TaapAction taapAction 

Date dateCreated // updated by grails 
Date lastUpdated // updated by grails 

User createdBy 
OrgUnit orgUnit 

Client client 
static belongsTo = [Client] 

static constraints = { 
} 

} 

TaapAction { 

int id 
User createdUser 
User responsibleUser 
Brand brand 
BusinessType businessType 
Topic topic 
Topic subTopic 
String subject 
String description 
Date targetDate 
int progress 
String responsible 
Client client 
static belongsTo = [Client] 
OrgUnit orgUnit 
Date dateCreated // updated by grails 
Date lastUpdated // updated by grails 
TaapActionState taapActionState 

static constraints = { 
    subject nullable: false, size: 1..64 
    description nullable: false, size: 1..4000 
    responsible nullable: false, size: 1..512 
    progress nullable: false 
    responsibleUser nullable:false 
    brand nullable:false 
    businessType nullable:false 
    topic nullable:false 
    subTopic nullable:false 
    targetDate nullable:false 
} 

TaCustomerBoard有與上述類似的約束。 但它給出異常而不是錯誤代碼。 下面是控制器Post方法:

def saveTaCustomerBoardAction(TaCustomerBoardActionCommand cmd){ 

    if(cmd.validate()){ 
     taActionPlanningService.saveAction(cmd.action.taapAction) 
     cmd.action.save(flush: true, failOnError: true) 
    } 
    [cmd:cmd] 
} 

堆棧跟蹤:

grails.validation.ValidationException:驗證錯誤(S)保存()過程中發生 : - 在對象「去字段錯誤。 idare.move.taap.TaapAction'on field'progress':rejected value [null];代碼 [de.idare.move.taap.TaapAction.progress.typeMismatch.error,de.idare.move.taap.TaapAction.progress.typeMismatch,taapAction.progress.typeMismatch.error,taapAction.progress.typeMismatch,typeMismatch.de .idare.move.taap.TaapAction.progress,typeMismatch.progress,typeMismatch.int,typeMismatch]; 爭論[progress];默認消息[數據綁定失敗] - 字段'description'的對象'de.idare.move.taap.TaapAction'中的字段錯誤:rejected value [null];代碼 [de.idare.move.taap.TaapAction.description.nullable.error.de.idare.move.taap.TaapAction.description,de.idare.move.taap.TaapAction.description.nullable.error.description,de .idare.move.taap.TaapAction.description.nullable.error.java.lang.String,de.idare.move.taap.TaapAction.description.nullable.error,taapAction.description.nullable.error.de.idare.move .taap.TaapAction.description,taapAction.description.nullable.error.description,taapAction.description.nullable.error.java.lang.String,taapAction.description.nullable.error,de.idare.move.taap.TaapAction.description .nu​​llable.de.idare.move.taap.TaapAction.description,de.idare.move.taap.TaapAction.description.nullable.description,de.idare.move.taap.TaapAction.description.nullable.java.lang.String ,de.idare.move.taap.TaapAction.description.nullable,taapAction.description.nullable.de.idare.move.taap.TaapAction.description,taapAction.description.nullable.description,taapAction.description.nullable.java.lang .Stri納克,taapAction.description.nullable,nullable.de.idare.move.taap.TaapAction.description,nullable.description,nullable.java.lang.String,可爲空]。 arguments [description,class de.idare.move.taap.TaapAction];默認 消息[類[{1}]的屬性[{0}]不能爲空]

請幫我解決這個問題。

+0

您正在使用哪個版本的Grails? –

回答

0

你的問題是相當直接的。那麼看起來,你已經提供瞭如何工作,但實際上並沒有提供什麼發送。我的建議是使用驗證方法在控制器操作中執行println params以查看它發送/驗證的內容。

您已聲明進度爲int而不是Integer。這意味着它不能爲空。總是使用布爾整數或任何情況下,如果某件事情可能是空的。其次,你還宣稱描述和進展是可以提供的虛假含義。錯誤消息建議發送的命令沒有發送給它的進度或描述作爲驗證的一部分。這是你需要通過簡單的調試進一步調查,如println在你的最後,以弄清楚爲什麼是這種情況。

int progress 
... 
static constraints = { 
    progress nullable: false 
    description nullable: false, size: 1..4000 
} 
+0

PARAMS是好的,當我檢查'cmd.errors'它顯示0 errors.But上保存它給出驗證異常。 – mansoor67890

+0

我會嘗試自己在saveCustomer方法內創建Command對象,而不是讓Grails自動爲您創建它。我發現在某些情況下,Grails可能會犯這樣的錯誤。 '''''''''''''''''''''''''''''''' –

0

只要刪除failOnError: true即可。您將能夠處理錯誤對象而不是捕獲異常。

Documentation