我如何解析数组错误列表并将其显示为验证错误

时间:2012-04-04 15:15:04

标签: grails gorm gsp grails-2.0 grails-controller

你解析一个验证错误的arraylist和diaply。我有一个arraylist

[passwordinsufficientuniquechar, passwordmaxrepeat, passwordinsufficientuniqueno, passwordnotenoughnumbers]

我在message.properties中有相应的消息,比如

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).

我正在使用网络流程。那么如何将这些消息解析为o / p以显示来自我的属性文件的消息。

1 个答案:

答案 0 :(得分:1)

Grails惯例是将您的邮件放在grails-app/i18n/messages.properties中。然后在您的观看中,您可以使用g:message标记:

<g:message code="passwordtooshort"/>

如果你有一组消息代码,你可以这样做:

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

视图通常是执行此操作的最佳位置,但如果您需要在控制器内部进行翻译,则可以这样做:

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