用於驗證和映射 Scalatra 請求參數的函式庫。
注意:該項目已合併到Scalatra中,不再維護!請參閱 Scalatra 文件以了解詳細資訊。
首先,將以下依賴項新增至 build.sbt 以使用 scalatra-forms。
libraryDependencies += " io.github.gitbucket " %% " scalatra-forms " % " 1.1.0 "
接下來,將ValidationJavaScriptProvider
加入 Scalatra 應用程式的 Bootstrap 中。
import io . github . gitbucket . scalatra . forms . _
class ScalatraBootstrap extends LifeCycle {
override def init ( context : ServletContext ) {
...
context.mount( new ValidationJavaScriptProvider , " /assets/js/* " )
...
}
}
scalatra-forms 現已準備就緒。
定義表單映射。它與 Play2 類似,但 scalatra-forms 更靈活。
import io . github . gitbucket . scalatra . forms . _
case class RegisterForm ( name : String , description : String )
val form = mapping(
" name " -> text(required, maxlength( 40 )),
" description " -> text()
)( RegisterForm .apply)
接下來,建立一個擴充 ScalatraServlet(或 ScalatraFilter)的 servlet(或過濾器)。它也混合在FormSupport
或ClientSideValidationFormSupport
中。映射請求參數的物件作為操作的參數傳遞。
class RegisterServlet extends ScalatraServlet with ClientSideValidationFormSupport {
post( " /register " , form) { form : RegisterForm =>
...
}
}
在 HTML 中,您必須執行以下兩件事。
<script>
來導入validation.js所需的jQuery<script>
導入validation.js,這有助於ValidationJavaScriptProvider
提供的客戶端驗證validation="true"
加入您的<form>
scalatra-forms 註冊一個提交事件監聽器來驗證表單內容。此監聽器將所有表單內容發佈到FORM_ACTION/validate
。此操作由 scalatra-forms 自動註冊以驗證表單內容。它以 JSON 形式傳回驗證結果。
在客戶端,scalatra-forms 將錯誤訊息放入span#error-FIELD_NAME
中。
< script src = " http://code.jquery.com/jquery-2.0.3.min.js " ></ script >
< script src = " /assets/js/validation.js " ></ script >
...
< form method = " POST " action = " /register " validation = " true " >
Name : < input type = " name " type = " text " >
< span class = " error " id = " error-name " ></ span >
< br />
Description : < input type = " description " type = " text " >
< span class = " error " id = " error-description " ></ span >
< br />
< input type = " submit " value = " Register " />
</ form >
您可以建立自訂Constraint
。
def identifier : Constraint = new Constraint (){
override def validate ( name : String , value : String ) : Option [ String ] =
if ( ! value.matches( " ^[a-zA-Z0-9 \ -_]+$ " )){
Some ( s " ${name} contains invalid character. " )
} else {
None
}
}
val form = mapping(
" name " -> text(required, identifier),
" description " -> text()
)( RegisterForm .apply)
您也可以透過重寫validate(String, String, Map[String, String])
建立多重欄位驗證器。可以透過params
查找其他欄位值。
創建多字段驗證器的其他方法是呼叫映射verifying
。您可以給函數驗證映射的案例類別。此函數採用映射值並傳回包含錯誤或Nil
Seq[(String, String)]
。
val form = mapping(
" reason " -> number(required),
" description " -> optional(text)
)( ReasonForm .apply).verifying { value =>
if (value.reason == 4 && value.descripsion){
Seq ( " description " -> " If reason is 'Other' then description is required. " )
} else {
Nil
}
}
對於 Ajax 操作,請使用ajaxGet
或ajaxPost
而不是get
或post
。由ajaxGet
或ajaxPost
定義的操作將驗證結果作為 JSON 回應傳回。
class RegisterServlet extends ScalatraServlet with ClientSideValidationFormSupport {
ajaxPost( " /register " , form) { form : RegisterForm =>
...
}
}
在客戶端,您可以使用displayErrors()
呈現錯誤訊息。
$ ( '#register' ) . click ( function ( e ) {
$ . ajax ( $ ( this ) . attr ( 'action' ) , {
type : 'POST' ,
data : {
name : $ ( '#name' ) . val ( ) ,
description : $ ( '#description' ) . val ( )
}
} )
. done ( function ( data ) {
$ ( '#result' ) . text ( 'Registered!' ) ;
} )
. fail ( function ( data , status ) {
displayErrors ( $ . parseJSON ( data . responseText ) ) ;
} ) ;
} ) ;
io.github.gitbucket
。put()
、 delete()
、 ajaxPut()
和ajaxDelete()
加入ClientSidevalidationFormSupport
。long
值類型的錯誤。%s
)與預設訊息相同。dummy()
錯誤。long
值類型。list
錯誤。dummy
值類型。MappingValueType
的verifying()
並刪除MappingConstraint
而不是它。length
約束。MappingConstraint
以透過MappingValueType
驗證轉換後的物件。SingleValueType
新增list()
映射。ValidationJavaScriptProvider
為validation.js 新增Content-Type 標頭。oneOf()
約束來檢查該值是否為指定字串之一。number()
和double()
的錯誤訊息。double()
和date()
映射。ValidationJavaScriptProvoider
。list()
映射。validate(String, String, Map[String, String])
加入到Constraint
。它使得可以在單一字段驗證中存取其他參數。verify()
加入MappingValueType
以驗證映射的實例。