I have a login form in view, there is a name input has many validators:
object Users extends Controller {
val loginForm = Form(tuple(
"name" -> (
nonEmptyText // (1)
verifying ("Its length should >= 4", name=>{ println("#222");name.length>=4 }) // (2)
verifying ("It should have numbers and letters", name=>{println("#333"); ...}) // (3)
)
}
Then I don’t input anything, press submit, and I found the console prints:
#222
#333
That means all validators performed, and they have relationship:
(1) & (2) & (3)
But I hope they:
(1) && (2) && (3)
That means, if the name is empty, the later two validators will be ignored.
Is it possible in play2?
The default behavior is to apply all the constraints defined on a field.
However, you can define your own validation constraint that stops applying constraints on the first fail:
It can be used like the following:
(Note that my implementation of
stopOnFirstFailapplies two times the failing constraint, so this one should not have side effects)