Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
amador-duran-toro opened this issue May 7, 2020 · 0 comments

Comments

@amador-duran-toro
Copy link

amador-duran-toro commented May 7, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant