Associating data with enumeration per command in Grails

I have a class:

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

where Foo is an enumeration:

 class Foo { A, B, C, D} 

I have a controller action with a parameter of type User

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

I created a multi-select in GSP

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

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

+4
source share
2 answers

http://www.grails.org/TipsAndTricks

Using an enumeration

If you want to use Enum with the String attribute "value" (a fairly common idiom) in an element, try the following:

 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 the key = "key" parameter to your tag. Credit: Gregg Bolinger

+2
source

In fact, this will not be the answer. But I can not post the details of this in a comment. I just created the following:

 enum State { OK,KS,FL,MA } class User { Set<State> states = [] static constraints = { } } <g:form controller="home" action="save"> <g:select name="states" multiple="true" from="${com.orm.fun.State.values()}"/> <g:submitButton name="save" value="Save"/> </g:form> // controller action def save = { User user -> // I didn't do anything here except // set a breakpoint for debugging } 

And here is what I get:

Debugging in IDEA

So, I'm not quite sure what distinguishes yours and mine, except for the listing name. Do you see anything?

0
source

Source: https://habr.com/ru/post/1369558/


All Articles