用于验证和映射 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以验证映射的实例。