과 함께 orderBy
이 제출되지 않은 경우 error.required
키를 사용하여 FormError
을 반환합니다.
숫자 대신 빈 String
이 제출 된 경우를 의미한다고 가정합니다. 귀하의 시도에 대한 문제는 String
빈이 number
유효성 검사기를 지나치지 않기 때문에 verifying
기능에 도달하지 못한다는 것입니다.
제가 생각할 수있는 유일한 것은 사용자 정의 Mapping[Int]
을 작성한 후 필드가 비어 있는지 먼저 확인한 다음 유효한 Int
인지 확인합니다.
val requiredNumber: Mapping[Int] = Forms.nonEmptyText
.verifying("Must be numeric", i => Try(i.toInt).isSuccess || i.isEmpty)
.transform[Int](_.toInt, _.toString)
그리고 테스트 :
scala> val form = Form(mapping("orderBy" -> requiredNumber)(identity)(Some(_)))
scala> form.bind(Map("orderBy" -> "1")).value
res24: Option[Int] = Some(1)
scala> form.bind(Map("orderBy" -> "")).errors
res26: Seq[play.api.data.FormError] = List(FormError(orderBy,List(error.required),WrappedArray()))
scala> form.bind(Map("orderBy" -> "aa")).errors
res27: Seq[play.api.data.FormError] = List(FormError(orderBy,List(Must be numeric),WrappedArray()))
scala> form.bind(Map("orderByzzz" -> "2")).errors
res28: Seq[play.api.data.FormError] = List(FormError(orderBy,List(error.required),List()))