【问题标题】:how to add alphanumeric field in play framework如何在播放框架中添加字母数字字段
【发布时间】:2017-05-26 12:48:02
【问题描述】:

我需要添加字母数字字段,因为我正在尝试此代码

object TestValidation {
  implicit val readTestUser: Reads[TestValidation] = (
    (JsPath \ "firstName").read(minLength[String](1)) and
    (JsPath \ "lastName").read(minLength[String](1)) and
    (JsPath \ "email").read(email) and
    (JsPath \ "password").read(minLength[String](1)))(TestValidation.apply _)

我希望“密码”字段为字母数字 我已经添加了这个自定义验证约束,现在我想在 json 的 Reads 方法中集成这个,可能会做这样的事情

 (JsPath \ "password").read(minLength[String](1)).passwordCheckConstraint

我不知道这样做的正确方法 这是约束代码

val allNumbers = """\d*""".r
val allLetters = """[A-Za-z]*""".r
val passwordCheckConstraint: Constraint[String] = Constraint("constraints.passwordcheck")({
  plainText =>
    val errors = plainText match {
      case allNumbers() => Seq(ValidationError("Password is all numbers"))
      case allLetters() => Seq(ValidationError("Password is all letters"))
      case _ => Nil
    }
    if (errors.isEmpty) {
      Valid
    } else {
      Invalid(errors)
    }
})

请帮忙

【问题讨论】:

    标签: java scala playframework playframework-2.5 playframework-json


    【解决方案1】:

    将约束表示为类型通常是一种很好的做法:

    import play.api.data.validation._
    import play.api.libs.json._
    
    class Password private(val str: String)
    
    object Password {
    
      val passwordCheckConstraint: Constraint[String] = Constraint("constraints.passwordcheck")({
        plainText =>
          val allNumbers = """\d*""".r
          val allLetters = """[A-Za-z]*""".r
          val lengthErrors = Constraints.minLength(1).apply(plainText) match {
            case Invalid(errors) => errors
            case _ => Nil
          }
          val patternErrors: Seq[ValidationError] = plainText match {
            case allNumbers() => Seq(ValidationError("Password is all numbers"))
            case allLetters() => Seq(ValidationError("Password is all letters"))
            case _ => Nil
          }
    
          val allErrors = lengthErrors ++ patternErrors
    
          if (allErrors.isEmpty) {
            Valid
          } else {
            Invalid(allErrors)
          }
      })
    
      def validate(pass: String): Either[Seq[ValidationError],Password] = {
        passwordCheckConstraint.apply(pass) match {
          case Valid => Right(new Password(pass))
          case Invalid(errors) => Left(errors)
        }
      }
    
      implicit val format: Format[Password] = Format[Password](
        Reads[Password](jsv => jsv.validate[String].map(validate).flatMap {
          case Right(pass) => JsSuccess(pass)
          case Left(errors) => JsError(Seq((JsPath \ 'password,errors)))
        }),
        Writes[Password](pass => Json.toJson(pass.str))
      )
    }
    

    现在你可以写这些了:

        (JsPath \ 'password).read[Password] //return Password instance or errors
        //or if you want to stick with the String type you can write this: 
        (JsPath \ 'password).read[Password].map(_.str)
    

    请注意,play-jsonJsPath.read 方法只接受一个类型参数,并且与 html 表单验证不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-23
      • 2013-03-05
      • 1970-01-01
      • 2014-10-08
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多