Displaying Grails Field Errors

后端 未结 2 1446
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-07 11:28

Does anybody know how I could get the fieldError to print out in the example below.

for each item with an error, I would like to print custom error messages

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-07 12:12

    You need access to the messageSource bean, e.g. with

    def messageSource
    

    in your controller or service. Then you can access the messages with

    def locale = Locale.getDefault()
    for (fieldErrors in bean.errors) {
       for (error in fieldErrors.allErrors) {
          String message = messageSource.getMessage(error, locale)
       }
    }
    

提交回复
热议问题