如何在播放框架中添加字母数字字段 [英] how to add alphanumeric field in play framework

查看:76
本文介绍了如何在播放框架中添加字母数字字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为此添加字母数字字段

i need to add alphanumeric field in play for that i am trying this code

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方法期间进行此操作

i want the "password" field to be alphanumeric i have added this custom validation constraint now i want to intregate this during Reads method of json doing something like this maybe

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

我不知道执行此操作的正确方法 这是约束代码

i don't know the correct way to do this here is the constraint code

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)
    }
})

请帮助

推荐答案

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

Having constraints represented as types is generally a very good practice:

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))
  )
}

有了这些,您现在可以编写:

With these in place now you can write:

    (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表单验证不同.

Note that the play-json's JsPath.read method accepts only a single type argument and is not the same as html form validations.

这篇关于如何在播放框架中添加字母数字字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆