Why the template doesn’t display a special Playframework authentication message

I am trying to do some custom validation using the playback platform, but it seems like I cannot get the error from the template.

Controller Code:

User user = User.findByEmail(email); if(user != null) { Logger.warn("User account already created for email %s", email); validation.addError("email", "This email address already in use."); params.flash(); flash.error("Please correct the error below!"); signup(); } 

and signup.html template:

  # {error 'email' /} 

I see that the controller sees a duplicate email, but the error message does not appear in the template.

Is the code correct?

+3
source share
1 answer

As you go to another view (i.e. you are redirected back to the registration view), Play performs the redirection, which means that the errors are no longer in scope, since the registration view is treated as a new request.

To get around this, you need to save validation messages for the next request, which is achieved using the validation.keep() function.

So, change your code, so just before you call signup() , you call validation.keep() .

Your code should look like

 if(user != null) { Logger.warn("User account already created for email %s", email); validation.addError("email", "This email address already in use."); params.flash(); flash.error("Please correct the error below!"); validation.keep(); signup(); } 
+6
source

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


All Articles