Grails bind request parameters to enum

后端 未结 2 1525
暖寄归人
暖寄归人 2021-02-05 22:20

My Grails application has a large number of enums that look like this:

public enum Rating {
    BEST(\"be\"), GOOD(\"go\"), AVERAGE(\"av\"), BAD(\"ba\"), WORST(\         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-05 22:40

    So the default Databinding binds on the Enum name and not a separately defined property of the Enum. You can either create your own PropertyEditor as you have mentioned or do a work-around similar to this:

    class MyCommand {
     String ratingId
        Rating getRating() {
         return Rating.getEnumFromId(this.ratingId)
        }
        static constraints = {
         ratingId(validator:{val, obj -> Rating.getEnumFromId(val) != null })
        }
    }
    

提交回复
热议问题