Search code examples
grailsgrails-ormgspgrails-2.0grails-controller

how do i parse a array list of errors and display it as validation errors


Hi parse a arraylist of validation errors and diaply that. I have a arraylist

[passwordinsufficientuniquechar, passwordmaxrepeat, passwordinsufficientuniqueno, passwordnotenoughnumbers]

and i have the corresponding messages in message.properties like

passwordcontainsusername=Your new password cannot contain your user name.
passwordtooshort=Your new password must be at least 8 characters long.
passwordtoolong=Your new password cannot exceed 50 characters.
password.change.different=The new password and the confirmed password values do not match.
passwordmaxrepeat=Your new password cannot contain more than 4 instances of the same character.
passwordequalsoldpassword=Your new password cannot be a previously used password.
passwordnotenoughnumbers=Your new password must contain at least 1 number or punctuation character.
passwordnotallowedchar=Your new password contain one or more characters that are not allowed.
password.change.validateerror=The account password and the current password do not match.
passwordnotenoughchars=Your new password must contain at least 2 letters.
passwordlessthan24hours=You cannot change your password more than three times in 24 hours.
passwordinsufficientuniquechar = Your new password must contain at least 5 unique characters.
passwordinsufficientuniqueno =Your new password must contain at least 2 unique numbers (symbols count as numbers).

I am using a webflow. So how do I parse those messages to o/p to display messages from my properties file.


Solution

  • The grails convention is to put your messages in grails-app/i18n/messages.properties. Then in your views you can use the g:message tag:

    <g:message code="passwordtooshort"/>
    

    If you have an array of message codes, you can do it like this:

    <g:each in="${messageCodes}">
        <g:message code="${it}"/>
    </g:each>
    

    The views are typically the best place to do this, but if you need to do a translation inside of a controller, you can do it like so:

    def translation = message(code: 'passwordtooshort')  // single code
    def translations = messageCodes.collect { message(code: it) } // list of codes