0

我必須檢查密碼字段是字母數字還是不是,它會拋出自定義驗證錯誤我使用的是播放框架,但得到編譯時錯誤如何檢查給定的字段是否字母數字或不在PlayFramework

value checkAlphanumeric is not a member of 
    play.api.libs.json.Reads[String] 
    - value checkAlphanumeric is not a member of 
    play.api.libs.json.Reads[String] 

我無法才達到我想要的結果我這樣做不對,這就是爲什麼我在這裏需要的是代碼

case class userUserSignUpValidation(firstName: String, 
             lastName: String, 
             email: String, 
             password: String) extends Serializable 

object UserSignUpValidation { 
    val allNumbers = """\d*""".r 
    val allLetters = """[A-Za-z]*""".r 
    var validationErrorMsg="" 
    implicit val readDirectUser: Reads[DirectUserSignUpValidation] = (
    (JsPath \ "firstName").read(minLength[String](1)) and 
    (JsPath \ "lastName").read(minLength[String](1)) and 
    (JsPath \ "email").read(email) and 
    (JsPath \ "password").read(minLength[String](8)checkAlphanumeric))(UserSignUpValidation.apply _) 
    def checkAlphanumeric(password:String)={ 
     val allNumbers = """\d*""".r 
     val allLetters = """[A-Za-z]*""".r 

     val errors = password match { 
      case allNumbers() => Seq(ValidationError("Password is all numbers")) 
      case allLetters() => Seq(ValidationError("Password is all letters")) 
      case _ => Nil 
     } 
    } 

我收到這條線上的錯誤

(JsPath \ "password").read(minLength[String](8)checkAlphanumeric))(UserSignUpValidation.apply _) 

究竟是爲什麼你不運行的情況下類中的驗證來實施上述方案

回答

0

你的問題是你不能使用你的checkAlphanumeric方法途徑。你可能想要的是在Reads一個過濾器,所以我建議做一些如下(我改變了檢查的實施,使用預先存在的方法):

implicit val readDirectUser: Reads[DirectUserSignUpValidation] = (
    (JsPath \ "firstName").read(minLength[String](1)) and 
    (JsPath \ "lastName").read(minLength[String](1)) and 
    (JsPath \ "email").read(email) and 
    (JsPath \ "password").read(minLength[String](8). 
     filterNot(ValidationError("Password is all numbers"))(_.forall(_.isDigit)). 
     filterNot(ValidationError("Password is all letters"))(_.forall(_.isLetter)) 
    )) (UserSignUpValidation.apply _) 
+0

由於它的工作:) – swaheed

0

嗯,我不知道正確的方式?

case class userUserSignUpValidation(firstName: String, 
             lastName: String, 
             email: String, 
             password: String) { 

    assert(!password.matches("""[A-Za-z]*""") && !password.matches("""\d*"""), "Invalid password") 
    // have other field validations here 
} 

而且在你UserSignUpValidation使用隱式的格式是這樣的:

object UserSignUpValidation { 
    implicit val userFormatter = JSON.format[userUserSignUpValidation] 

    // de-serialization code here 
} 
相關問題