mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-01 11:06:06 +01:00
Separate extension of scalatra-forms to trait from ControllerBase.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import model.Account
|
import model.Account
|
||||||
|
import util.Validations
|
||||||
import org.scalatra._
|
import org.scalatra._
|
||||||
import org.scalatra.json._
|
import org.scalatra.json._
|
||||||
import org.json4s._
|
import org.json4s._
|
||||||
@@ -9,7 +10,8 @@ import jp.sf.amateras.scalatra.forms._
|
|||||||
/**
|
/**
|
||||||
* Provides generic features for ScalatraServlet implementations.
|
* Provides generic features for ScalatraServlet implementations.
|
||||||
*/
|
*/
|
||||||
abstract class ControllerBase extends ScalatraFilter with ClientSideValidationFormSupport with JacksonJsonSupport {
|
abstract class ControllerBase extends ScalatraFilter
|
||||||
|
with ClientSideValidationFormSupport with JacksonJsonSupport with Validations {
|
||||||
|
|
||||||
implicit val jsonFormats = DefaultFormats
|
implicit val jsonFormats = DefaultFormats
|
||||||
|
|
||||||
@@ -33,31 +35,6 @@ abstract class ControllerBase extends ScalatraFilter with ClientSideValidationFo
|
|||||||
url.substring(0, url.length - request.getRequestURI.length)
|
url.substring(0, url.length - request.getRequestURI.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Constraint for the identifier such as user name, repository name or page name.
|
|
||||||
*/
|
|
||||||
protected def identifier: Constraint = new Constraint(){
|
|
||||||
def validate(name: String, value: String): Option[String] =
|
|
||||||
if(!value.matches("^[a-zA-Z0-9\\-_]+$")){
|
|
||||||
Some("%s contains invalid character.".format(name))
|
|
||||||
} else if(value.startsWith("_") || value.startsWith("-")){
|
|
||||||
Some("%s starts with invalid character.".format(name))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ValueType for the Date property.
|
|
||||||
*/
|
|
||||||
def date(constraints: Constraint*): SingleValueType[java.util.Date] =
|
|
||||||
new SingleValueType[java.util.Date]((pattern("\\d{4}-\\d{2}-\\d{2}") +: constraints): _*){
|
|
||||||
def convert(value: String): java.util.Date = {
|
|
||||||
val formatter = new java.text.SimpleDateFormat("yyyy-MM-dd")
|
|
||||||
formatter.parse(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case class Context(path: String, loginAccount: Option[Account])
|
case class Context(path: String, loginAccount: Option[Account])
|
||||||
33
src/main/scala/util/Validations.scala
Normal file
33
src/main/scala/util/Validations.scala
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import jp.sf.amateras.scalatra.forms._
|
||||||
|
import scala.Some
|
||||||
|
|
||||||
|
trait Validations {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constraint for the identifier such as user name, repository name or page name.
|
||||||
|
*/
|
||||||
|
def identifier: Constraint = new Constraint(){
|
||||||
|
def validate(name: String, value: String): Option[String] =
|
||||||
|
if(!value.matches("^[a-zA-Z0-9\\-_]+$")){
|
||||||
|
Some("%s contains invalid character.".format(name))
|
||||||
|
} else if(value.startsWith("_") || value.startsWith("-")){
|
||||||
|
Some("%s starts with invalid character.".format(name))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ValueType for the java.util.Date property.
|
||||||
|
*/
|
||||||
|
def date(constraints: Constraint*): SingleValueType[java.util.Date] =
|
||||||
|
new SingleValueType[java.util.Date]((pattern("\\d{4}-\\d{2}-\\d{2}") +: constraints): _*){
|
||||||
|
def convert(value: String): java.util.Date = {
|
||||||
|
val formatter = new java.text.SimpleDateFormat("yyyy-MM-dd")
|
||||||
|
formatter.parse(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user