(nonEmptyText verifying...">

How to display global form errors on different inputs?

I have a registration form:

val loginForm = Form(tuple( "email" -> (nonEmptyText verifying email.constraints.head), "password" -> nonEmptyText ) .verifying("Email doesn't exist", params => User.findByEmail(params._1) != None) .verifying("Password incorrect", params => User.findByEmail(params._1).map(_.checkPassword(params._2)) == Some(true)) ) 

Note the two global validators in the latter. They should be executed only if the email not empty and has a valid format, and the password not empty, so I put it in the global one.

I want to display Email doesn't exist next to the email input and Password incorrect next to the password input, how to do this in the field of view?

I am currently using loginForm.globalError , but it will show both of them next to the same input.

 @inputText(loginForm("email"), '_label->"Email:", '_error->loginForm.globalError ) @inputPassword(loginForm("password"), '_label->"Password:") 
+6
source share
2 answers

IMHO, the global error should remain global, so I would put it above your inputs:

 @loginForm.globalError.map { error => <div>@error</div> } @inputText(loginForm("email"), '_label->"Email:") @inputPassword(loginForm("password"), '_label->"Password:") 

Otherwise, you will need to do something like this:

 '_error -> loginForm.error("email").orElse(globalError) 
+6
source

I think that the email restriction should be defined in the email field, and not worldwide. And consider that the password restriction is global as it checks the pair (email address, password).

+1
source

Source: https://habr.com/ru/post/910987/


All Articles