Reputation: 401
I don't manage to access to the ValidationError message from my view. The only output I manage to get in my view is :
[ValidationError(username,Too short, sorry ;),[6])]
[ValidationError(password,Confirmation password doesn't match,[])]
I would like to output only the error message : "Too short, sorry ;)" and "Confirmation password doesn't match".
The workaround I found is by calling a specific field from the form and then access to the error message :
@form("password").error.map(_.message).getOrElse("")
Thanks,
My view register.scala.html :
@if(form.hasErrors) {
<div class="form-group">
<div class="alert alert-danger col-lg-6 text-center">
@for((key, vamlue) <- form.errors){
@value<br />
}
</div>
</div>
}
My controller :
public static Result registerSubmit(){
Form<User> registerForm = form(User.class).bindFromRequest();
String passwordConfirmation = registerForm.field("passwordConfirmation").value();
if(!registerForm.field("password").valueOr("").equals(passwordConfirmation)){
ValidationError e = new ValidationError("password", "Confirmation password doesn't match");
registerForm.reject(e);
}
if (registerForm.hasErrors()){
// Handle Error
return badRequest(register.render(registerForm));
} else {
// Check if all data are fine
// TODO : Redirect to Login Page
return ok(register.render(registerForm));
}
}
And finally the model User.java :
@Required
@MinLength(6)
String username;
@Required
@Email
String email;
@Required
@MinLength(6)
String password;
Upvotes: 2
Views: 1618
Reputation: 2708
It sounds like you want to collect all your form errors and display them at the top of the form. I think this is the code block you want to be using in your HTML template
@if(form.hasErrors) {
<div class="form-group">
<div class="alert alert-danger col-lg-6 text-center">
@for(entry <- form.errors.entrySet){
@for(error <- entry.getValue){
@error.message <br/>
}
}
</div>
</div>
}
If you convert the form.errors
map from a Java map to a Scala Map you can make your code block a little more concise than nested for-loops, but this should do the job.
Upvotes: 5