kofhearts
kofhearts

Reputation: 3794

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:

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?

Upvotes: 0

Views: 496

Answers (1)

kofhearts
kofhearts

Reputation: 3794

Turns out that I was using the wrong message code. Here is the message code for min property.

className.propertyName.min.notmet

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

Upvotes: 1

Related Questions