- Notifications
You must be signed in to change notification settings - Fork5
gitbucket/scalatra-forms
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A library to validate and map request parameters for Scalatra.
Notice: This project is maintained no longer because it has been merged into Scalatra! Seethe Scalatra documentation to know details.
At first, add the following dependency into your build.sbt to use scalatra-forms.
libraryDependencies+="io.github.gitbucket"%%"scalatra-forms"%"1.1.0"
Next, addValidationJavaScriptProvider to Bootstrap of your Scalatra application.
importio.github.gitbucket.scalatra.forms._classScalatraBootstrapextendsLifeCycle {overridedefinit(context:ServletContext) { ... context.mount(newValidationJavaScriptProvider,"/assets/js/*") ... }}
scalatra-forms is now ready.
Define a form mapping. It's a similar to Play2, but scalatra-forms is more flexible.
importio.github.gitbucket.scalatra.forms._caseclassRegisterForm(name:String,description:String)valform= mapping("name"-> text(required, maxlength(40)),"description"-> text())(RegisterForm.apply)
Next, create a servlet (or filter) which extends ScalatraServlet (or ScalatraFilter).It also mixed inFormSupport orClientSideValidationFormSupport.The object which is mapped request parameters is passed as an argument of action.
classRegisterServletextendsScalatraServletwithClientSideValidationFormSupport { post("/register", form) {form:RegisterForm=> ... }}
In the HTML, you have to do two things below.
- Add
<script>to import jQuery which is required by validation.js - Add
<script>to import validation.js which helps client side validation provided byValidationJavaScriptProvider - Add
validation="true"to your<form>
scalatra-forms registers a submit event listener to validate form contents.This listener posts all the form contents toFORM_ACTION/validate.This action is registered by scalatra-forms automatically to validate form contents.It returns validation results as JSON.
In the client side, scalatra-forms puts error messages intospan#error-FIELD_NAME.
<scriptsrc="http://code.jquery.com/jquery-2.0.3.min.js"></script><scriptsrc="/assets/js/validation.js"></script>...<formmethod="POST"action="/register"validation="true">Name: <inputtype="name"type="text"> <spanclass="error"id="error-name"></span> <br/>Description: <inputtype="description"type="text"> <spanclass="error"id="error-description"></span> <br/> <inputtype="submit"value="Register"/></form>
You can create customConstraint.
defidentifier:Constraint=newConstraint(){overridedefvalidate(name:String,value:String):Option[String]=if(!value.matches("^[a-zA-Z0-9\\-_]+$")){Some(s"${name} contains invalid character.") }else {None }}valform= mapping("name"-> text(required, identifier),"description"-> text())(RegisterForm.apply)
You can also create multi field validator by overridingvalidate(String, String, Map[String, String]).It's possible to look up other field value viaparams.
Other way to create multi field validator is callingverifying for mapping.You can give the function yo validate the mapped case class. This function takes the mapped value and returnsSeq[(String, String)] which contains errors orNil.
valform= 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 }}
For the Ajax action, useajaxGet orajaxPost instead ofget orpost.Actions which defined byajaxGet orajaxPost return validation result as JSON response.
classRegisterServletextendsScalatraServletwithClientSideValidationFormSupport { ajaxPost("/register", form) {form:RegisterForm=> ... }}
In the client side, you can render error messages usingdisplayErrors().
$('#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));});});
- Scalatra 2.5.0 and Scala 2.12 support
- Move to GitBucket organization.
- Change group id and package name to
io.github.gitbucket. - Add
put(),delete(),ajaxPut()andajaxDelete()toClientSidevalidationFormSupport.
- Fix performance issue
- Support Scala 2.11 and Scalatra 2.3
- Bug fix about form state after after validation succeeded.
- Bug fix for
longvalue type.
- Formatting (such as
%s) is available in custom messages as same as default messages.
- Fix
dummy()bug.
- Add
longvalue type. - Fix
listbug for un-nested properties.
- Add
dummyvalue type.
- Fix nested property handling problem.
- Fix
verifying()forMappingValueTypeand removeMappingConstraintinstead of it. - Add
lengthconstraint.
- Add
MappingConstraintto validate converted object byMappingValueType.
- Add
list()mapping forSingleValueType. ValidationJavaScriptProvideradds Content-Type header for validation.js.- Fix to run parent validations before number checking.
- Add
oneOf()constraint which checks whether the value is one of specified strings. - Fix to retrieve error message for
number()anddouble()from ResourceBundle.
- Add
double()anddate()mapping. - Add I18N support for error messages.
- Add
ValidationJavaScriptProvoider. - Add
list()mapping for List property.
- Improved nested property support.
- Add
validate(String, String, Map[String, String])toConstraint.It makes possible to access other parameter in single field validation. - Add
verify()toMappingValueTypewhich validates the mapped instance.
- This is the first public release.
About
A library to validate and map request parameters for Scalatra.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors3
Uh oh!
There was an error while loading.Please reload this page.