gpc / fields

a spiritual successor to the bean-fields plugin
http://gpc.github.io/fields/
84 stars 106 forks source link

Collections of enums are not displayed in <f:all> in edit.gsp and create.gsp #306

Open amador-duran-toro opened 4 years ago

amador-duran-toro commented 4 years ago

In Grails 4.0.3 and Grails 3.3.11, when a class has a collection of enums as a field, e.g.

package mypackage

class MyClass {
  String name
  // ...
  // properties
  // ...
  Set<MyEnum> enums

  // no need to declare it as embedded
  // but you can do it if you like
  // static embedded = ['enums'] 
}

enum MyEnum {
  ENUM_VALUE_1,
  ENUM_VALUE_2,
  ENUM_VALUE_3
}

Or as a hasMany association, e.g.

package mypackage

class MyClass {
  String name
  // ...
  // properties
  // ...

  static hasMany = [enums:MyEnum]
}

enum MyEnum {
  ENUM_VALUE_1,
  ENUM_VALUE_2,
  ENUM_VALUE_3
}

And you populate the class in Bootstrap.groovy, e.g.

package mypackage

class BootStrap {

    def init = { servletContext ->
        MyClass.withTransaction{ status ->
            MyClass.saveAll(
                new MyClass(name:"c1").addToEnums(MyEnum.ENUM_VALUE_1),
                new MyClass(name:"c2").addToEnums(MyEnum.ENUM_VALUE_2),
                new MyClass(name:"c3").addToEnums(MyEnum.ENUM_VALUE_3)
            )
        }
    }
    def destroy = {
    }
}

If you use scaffolding, the content of the enum collection is shown in the index and show pages, but not in the edit and create pages, where only the label is shown, no widget is displayed for the field.

If you generate the views for the class, the fields are displayed using <f:all bean="myClass"/>.

There is a workaround for this problem, without having to generate the views, which is creating the grails-app/views/_fields/myClass/enums/_widget.gsp file with the following content:

<g:select 
    multiple="true" 
    name="${property}" 
    from="${mypackage.MyEnum}"
    value="${myClass?.enums}"
/>

I think that this should be the default behaviour for collections of enums in scaffolding, but I do not know how to implement it using the fields plugin for any collection of enums instead of for a given field only.