• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala Forms类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala Process类代码示例发布时间:2022-05-23
下一篇:
Scala Assert类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap