2012-05-24 74 views
0

我使用的播放框架2.0.1。我有一個jQuery的彈出式窗體,我必須在服務器端進行驗證,並顯示驗證消息WITHOUT CLOSING彈出。服務器端驗證彈出

我試圖用java腳本路線作爲禪宗任務例子給出的,但我覺得沒有例子,使從客戶端Ajax調用。

我找不到在遊戲框架2.0使用AJAX任何其他例子。

任何幫助表示讚賞。

謝謝。

回答

2

在客戶端,你可以調用自己的JavaScript功能,將收集所有的表單輸入:

$("#myForm").submit(function(evt) { 
    evt.preventDefault(); 
    $.ajax({ 
     type: "POST", 
     url: "@routes.MyController.myMethod()", 
     data: { username: $("#myForm #username").val(), age: $("#myForm #age").val() }, 
     dataType: "json", 
     success: function(data, textStatus, jqXHR) { 
      console.log("Success of call"); 
      //Do what you want here 
      //For example close the jquery opened panel 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
      console.log("Failure of call"); 
     } 
    } 
    return false; 
}); 

在服務器端,你將把傳入請求的任何其它的(這是斯卡拉例子,適應它爲Java):

def connectWithFB = Action { implicit request => 
    Form(tuple("username" -> nonEmptyText, "age" -> nonEmptyText)).bindFromRequest.fold(
     errors => { 
      BadRequest(toJson(Map("status" -> "0", "error" -> "Missing fields or wrong field"))) 
     }, 
     success => { 
      Ok(toJson(Map("status" -> "1"))) 
     } 
    ) 
} 
+0

非常感謝你爲你的即時響應。你能解釋一下如何調用java腳本方法嗎?因爲我是jquery的新手。我有以下代碼。 @ helper.form(動作= routes.Drabble.submit,「ID - > 「myForm會」){ @inputText(drabbleForm( 「標題」))},即使沒有輸入必填字段,彈出窗口也會關閉。請幫忙。 –

+0

你應該考慮在不同的StackOverflow線程中分割你的問題。 JavaScript,jQuery,Ajax,Play模板和Play控制器是非常不同的主題。如果你不瞭解其中的一個問題,就可以在SO上提出一個非常具體的問題。 – iwalktheline

+0

感謝kheraud的迴應。 –

0

自舉的那麼容易做到的驗證, 爲例:

<div class="form-group" style="position:relative;left:20px;"> 
    <label class="sr-only" for="exampleInputEmail2"><strong> Genre</strong> 
      </label> 
    <input type="text" name="genre" required /> 


    </div> 

所有你需要做的是說,it's需要像

<input type="text" name="genre" required /> 

乾杯