Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data binding to an enum on a command in Grails

I have a class:

class User {
    Set<Foo> foos = []
}

where Foo is an enum:

class Foo { A, B, C, D}

I have a controller action with a parameter of type User

def someAction = {User user ->
    // impl omitted   
}

I've created a multi-select in a GSP

<g:select name="foos" multiple="true" from="${Foo.values()}"/>

But when I submit the form the selected values do not get bound to the foos property of the User command object. What am I doing wrong?

like image 346
Dónal Avatar asked Nov 05 '22 15:11

Dónal


1 Answers

http://www.grails.org/TipsAndTricks

Enum usage

If you want to use a Enum with a "value" String attribute (a pretty common idiom) in a element, try this:

enum Rating {
    G("G"),PG("PG"),PG13("PG-13"),R("R"),NC17("NC-17"),NR("Not Rated")

    final String value

    Rating(String value) { this.value = value }

    String toString() { value }

    String getKey() { name() } 
}

Then add optionKey="key" to your tag. Credit: Gregg Bolinger

like image 183
Ray Tayek Avatar answered Nov 15 '22 07:11

Ray Tayek