【问题标题】:how do i parse a array list of errors and display it as validation errors如何解析错误数组列表并将其显示为验证错误
【发布时间】:2012-04-04 15:15:03
【问题描述】:

您好,解析验证错误的数组列表,然后将其解析。我有一个数组列表

[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 以显示来自我的属性文件的消息。

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • def 翻译有助于将属性文件中的所有消息放到对象中。如何将翻译对象绑定到命令对象并将其显示为错误消息??
    • 通常你对你的命令对象施加约束,当调用 validate 时 grails 会在命令对象上创建错误。命令类名与字段名和约束名相结合,构成在 messages.properties 中查找的键。
    • 是的,有效....我忘记将命令对象添加到 flow 。那是在制造错误。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2010-09-23
    • 2023-03-26
    • 1970-01-01
    • 2018-12-13
    • 2015-07-10
    相关资源
    最近更新 更多