apache / apache/grails-core

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

Open
#14,218 0 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-fields
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

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

```{groovy}
package mypackage

class MyClass {
String name
// ...
// properties
// ...
Set 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.

```{groovy}
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.

```{groovy}
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 ``.

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:

```{html}

```

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.

Contributor guide

Open the contributing guide

Research direction

Start by tracing how and the fields plugin select widgets for edit.gsp and create.gsp, comparing that path with the working index and show views. Inspect the example override at grails-app/views/_fields/myClass/enums/_widget.gsp; done means collection-of-enum fields render a multiple-selection widget by default without a field-specific override.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.