In this question: If a form field has multi validators, how to let play verify them one by one, not all?, Julien gave me a method named stopOnFirstFail to solve my problem:
def stopOnFirstFail[T](constraints: Constraint[T]*) = Constraint { field: T =>
constraints.toList dropWhile (_(field) == Valid) match {
case Nil => Valid
case constraint :: _ => constraint(field)
}
}
It’s usage is:
val loginForm = Form(
"name" -> (text verifying stopOnFirstFail( nonEmpty, minLength(4) ))
)
But I hope to define a dsl which can be used as:
val loginForm = Form(
"name" -> (text verifying ( nonEmpty or minLength(4) ))
)
I tried to defined an implicit method for play.api.data.validation.Constraint:
import play.api.data.validation._
implicit def _Constraint[T](cons: Constraint[T]) = new {
def or[T](other: Constraint[T]) = Constraint { field: T =>
cons(field) match { // (!)
case Valid => other(field)
case invalid => invlaid
}
}
}
But it can’t be compiled, the error in on the (!) line, and message is:
type mismatch;
found: field.type (with underlying type T) required: T
Note: implicit method _Constraint is not applicable here
because it comes after the application point and it lacks an explicit result type
How to fix it?
The
ormethod does not take a type parameter: