当前位置: 首页>>代码示例>>Scala>>正文


Scala Forms类代码示例

本文整理汇总了Scala中play.api.data.Forms的典型用法代码示例。如果您正苦于以下问题:Scala Forms类的具体用法?Scala Forms怎么用?Scala Forms使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Forms类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: ValidationUtils

//设置package包名称以及导入依赖的类
package utils

import java.time.LocalDate

import models.DateComponents
import org.apache.commons.lang3.StringUtils
import play.api.data.format.Formatter
import play.api.data.validation.{Constraint, Invalid, Valid, ValidationError}
import play.api.data.{FieldMapping, FormError, Forms}

import scala.util.{Failure, Success}

object ValidationUtils {

  implicit val mandatoryBooleanFormatter = new Formatter[Boolean] {

    def bind(key: String, data: Map[String, String]) = {
      Right(data.getOrElse(key, "")).right.flatMap {
        case "true" => Right(true)
        case "false" => Right(false)
        case _ => Left(Seq(FormError(key, s"$key.error.boolean", Nil)))
      }
    }

    def unbind(key: String, value: Boolean) = Map(key -> value.toString)
  }

  val mandatoryBoolean: FieldMapping[Boolean] = Forms.of[Boolean]
  val notBlank: (String) => Boolean = StringUtils.isNotBlank

  def unconstrained[T] = Constraint[T] { (t: T) => Valid }

  def inRange[T](minValue: T, maxValue: T, errorCode: String = "")(implicit ordering: scala.math.Ordering[T]): Constraint[T] =
    Constraint[T] { (t: T) =>
      assert(ordering.compare(minValue, maxValue) < 0, "min bound must be less than max bound")
      (ordering.compare(t, minValue).signum, ordering.compare(t, maxValue).signum) match {
        case (1, -1) | (0, _) | (_, 0) => Valid
        case (_, 1) => Invalid(ValidationError(s"error$errorCode.range.above", maxValue))
        case (-1, _) => Invalid(ValidationError(s"error$errorCode.range.below", minValue))
      }
    }

  def validDate(constraint: Constraint[LocalDate] = unconstrained) = Constraint[DateComponents] {
    (dcs: DateComponents) =>
      DateComponents.toLocalDate(dcs) match {
        case Failure(_) => Invalid(ValidationError("error.date.invalid", dcs))
        case Success(localDate) => constraint(localDate)
      }
  }

  def optionallyMatchingPattern(regex: String): Constraint[String] =
    Constraint[String] { s: String =>
      Option(s) match {
        case None | Some("") => Valid
        case _ if s.matches(regex) => Valid
        case _ => Invalid(ValidationError("error.string.pattern", s))
      }
    }

} 
开发者ID:PeterPerhac,项目名称:pocs,代码行数:61,代码来源:ValidationUtils.scala

示例2: FormMapping

//设置package包名称以及导入依赖的类
package controllers

import javax.inject.{Inject, Singleton}

import play.api.data.{Forms, Form}
import play.api.mvc.{Action, Controller}
import play.api.data.Forms._

@Singleton
class FormMapping @Inject() extends Controller {

  //Map to Form
  def save() = Action { request =>
    val data = Map(
      "name" -> "Box of paper clips",
      "ean" -> "1234567890123",
      "pieces" -> "300")

    val productForm = Form(
      Forms.tuple(
        "name" -> Forms.text,
        "ean" -> Forms.text,
        "pieces" -> Forms.number))

    val boundForm = productForm.bind(data)

    val success = boundForm.fold(_ => false, _ => true)

    boundForm.fold(
      formWithErrors => BadRequest,
      value => {
        doSomethingWithValue(value)
        Ok
      })
  }
  def doSomethingWithValue(a: Any) = {}

  // Mapping to Form
  case class User(username: String, realname: Option[String], email: String)

  val userForm = Form(
    mapping(
      "username" -> nonEmptyText(8),
      "realname" -> optional(text),
      "email" -> email)(User.apply)(User.unapply))

  def createUser() = Action { request =>
    val c = userForm.bindFromRequest()(request).fold(
      formWithErrors => Forbidden,
      user => Ok("User created!" + user))
    c
  }

  def createUserImplicit() = Action { implicit request =>
    val c = userForm.bindFromRequest.fold(
      formWithErrors => Forbidden,
      user => Ok("User created!" + user))
    c
  }

} 
开发者ID:zqhxuyuan,项目名称:first-player,代码行数:62,代码来源:FormMapping.scala


注:本文中的play.api.data.Forms类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。