We're trying to be consistent when we define our error messages for form validation / authorization, but we're finding it a bit hard to work with Play 2.0's @Constraints etc.
What we want is to be able to access each error (and it's relevant i18n-translation), preferably through an error object. This proved to be rather difficult however, as the errors (at least from @Constraint) are applied to their i18n-translations in the ImplicitFieldConstructor, which we don't really have much access to.
What we'd like to know is if there's a way to access all FieldElements of each field in a form without going through the ImplicitFieldConstructor? (hopelessly tried @helper.FieldElements)
of course we could just drop the entire @Constraints and just define our own validators and validation methods, but it seems kinda counter-intuitive since we want to use the framework for all that it's worth.
EDIT: Also, we tried this:
@for((key, value) <- loginForm.errors) {
@value.get(0)
}
But that just gives us the messages (for instance 'error.required') rather than the i18n-translation.
Edit
We use this for now as a hack to fix it, but I'd like to find a better way of handling this.
if (loginForm.hasErrors()) {
// TODO: find a better solution to handle this problem.
for(Map.Entry<String, List<ValidationError>> entry : loginForm.errors().entrySet() ) {
ValidationError oldError = entry.getValue().get(0);
// applay i18n-translation to error message.
ValidationError error = new ValidationError(oldError.key(), Messages.get(oldError.message()), new ArrayList());
entry.getValue().set(0, error);
}
return badRequest(index.render(loginForm));
Thanks!