开发者

how to change constraints errors messange in grails

开发者 https://www.devze.com 2023-01-01 18:17 出处:网络
I have domain with constraints like min value must greater than 0 I have no idea how to change the message if the constraints are not passed.

I have domain with constraints like min value must greater than 0

I have no idea how to change the message if the constraints are not passed.

which file i need to edit to do that ?

I also need to display the values some properties as well .. like

"you cannot make any transaction because your balance is less 开发者_运维问答than 100. Your current balance is ${currentBalance} after deducted. Your previous balance is ${previousBalance} and amount that need to be deducted ${deductedValue}"

note my class:

class Transaction
BigDecimal previousBalance
BigDecimal currentBalance
BigDecimal deductedValue ; 

constraints currentBalance(min:100)

beforeUpdate => currentBalance = previousBalance - deductedValue 


The docs give you an understanding of the correct Grails approach to validation error messages.

http://grails.org/doc/1.3.1/guide/7.%20Validation.html#7.4%20Validation%20and%20Internationalization

Also, check out this free ebook, a good introduction to the Grails fundamentals

http://www.infoq.com/minibooks/grails-getting-started

Specifically p28


Edit your messages.properties file in your grails-app/i18n folder with the messages corresponding with the constraint that was violated. For a plug-in that can help you generate the messages to go along with your constraints see this.


If you just want to check that a value is greater than a certain number, put a constraint in the domain class (myClass), such as:

static constraints = { myNumber(min:0) }

In messages.properties files add the line:

myClass.myNumber.min.notmet = There is a problem: The number must be greater than {3}.


Under grails-app/i18n use messages.properties to add your string (or any other locale you're working with).

If your domain class is called user and the field you have is called balance:

class User{
   static constraints = {
      balance(min:100)
    }
   BigDecimal balance;
}

then you'd add:

user.balance.min.error.User.balance= you cannot make any transaction because your balance is less than [{3}].Your current balance is [{2}]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号