簡體   English   中英

使用Ajax進行Grails命令對象驗證

[英]Grails command object validation with Ajax

在Grails項目中,我有一個命令對象來驗證提交的表單。 此命令obj。 也有自定義驗證器。 數據使用jQuery ajax($ .post)提交。

我的問題是我現在如何將使用jQuery Ajax發送的數據映射到Command對象中列出的屬性?

例如:

$.post('comment/save', {id: id, title: title});

然后在控制器中:

def save(saveCommand cmd){

  if (!cmd.validate()){
    ...
  }
}

class saveCommand {

   Comment comment_id    // maps to: params.comment_id send with ajax
   String title          // maps to: params.title send with ajax

   // constraints
   // validators    
}

comment_id屬性應該與從jQuery發送的參數映射中的“ comment_id”參數綁定,而不是像現在一樣作為“ id”綁定。

無論如何,我猜您有一個Comment域類,並且您希望從數據庫綁定該實體。 在這種情況下,應在參數名稱中添加“ .id”后綴。

$.post('comment/save', {"comment_id.id": id, "title": title});

PS:也許您想在命令類中將“ comment_id”重命名為“ comment”。 這樣做,您將必須在ajax請求中將參數名稱更改為“ comment.id”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM