Search code examples
validationplayframeworkaliases

Use Aliases for Validation to show gramatically correct error messages - play framework


My question refers to validation in a Model class. I know how to basically do validation, but I've though got a question about it.

I'd like to now if it is possible to use a alias in validation? Because it should display a german message, and mixed up with my English database field name it looks really strange.

So, without an alias my code in the model class would be

@Equals("password")
@Required
public String passwordConfirm;

So password has to equal with passwordConfirm (passwordConfirm = "Bestätigung des Passworts" in German, password="Passwort" :) Which would print, if a error occurs: "Bestätigung des Passworts muss mit password übereinstimmen." (should be "Passwort", not "password")

So do I need to define an alias or something, or how could this work?

greetings


Solution

  • You should be able to use the "message" variable for that, to override the output of the validation.

    @Equals({value="password", message="key.to.messages.i18n")
    

    This will let you define the validation messages in I18N files.

    Disclaimer: don't have the code here, but it's something like this, beware of typos :P