简体   繁体   English

如何为grails域对象的约束违反设置自定义错误消息?

[英]how to set custom error message for constraint violation of a grails domain object?

First of all the domain definition is as follows: 首先,域定义如下:

package com.abc.def

class EventDonation implements Serializable{

    String title
    String body
    BigDecimal customDonationMin
    BigDecimal customDonationMax


    static constraints = {  
        title blank: false, nullable: false
        body blank: false, nullable: false
        customDonationMin min: BigDecimal.ZERO
    }
}

The part in view page which renders the error is as follows: 视图页面中呈现错误的部分如下:

   <g:hasErrors bean="${donation}">                                                         
        <g:eachError var="error" bean="${donation}">
            <li><g:message error="${error}"/></li>
        </g:eachError>
    </g:hasErrors>      

Finally in message.properties the line added is as follows: 最后,在message.properties ,添加的行如下:

com.abc.def.eventDonation.customDonationMin.min = Minimum limit cannot be less than 0

But the error that is being shown is: 但是显示的错误是:

customDonationMin in class com.abc.def.EventDonation with value -10 is less than minimum value 0

do you know the reason why the custom error message is not being displayed? 您知道未显示自定义错误消息的原因吗?

Turns out that I was using the wrong message code. 原来我使用了错误的消息代码。 Here is the message code for min property. 这是min属性的消息代码。

className.propertyName.min.notmet

Reference: http://grails.github.io/grails-doc/2.2.1/ref/Constraints/min.html 参考: http : //grails.github.io/grails-doc/2.2.1/ref/Constraints/min.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM