|
Hi all,
I use grails 1.3.2 and hbase plugin.. I have a ColorVal domain class, which color field can have one of "blue", "red" and "green" values, so i want color to be enum type.. public enum ColorVal { BLUE("blue","blue"), RED("red",red), GREEN("green","green"), final String name; private ColorVal (String name) { this.name = name } String toString() { name } } //domain class Class ColorVal { .... ColortVal color } In ColortValController i have "new" action, which saves domain class's object in db... When i run .../myApp/ColorVal/new?color=blue, I get exception: Error 500: ...: Cannot cast object 'PAYPAL' with class 'java.lang.String' to class 'pack.ex.ColorVal' Please help me to understand my mistake.. |
|
sorry, exception is:Error 500: ...: Cannot cast object 'blue' with class 'java.lang.String' to class 'pack.ex.ColorVal'
On Sun, Mar 6, 2011 at 3:00 PM, hbbg [via Grails] <[hidden email]> wrote: Hi all, |
|
In reply to this post by hbbg
Try using an id property, like in the example here:
http://www.grails.org/1.1+Release+Notes#Enhanced%20Enum%20Support On 3/6/2011 3:00 AM, hbbg wrote: > Hi all, > I use grails 1.3.2 and hbase plugin.. > > I have a ColorVal domain class, which color field can have one of "blue", > "red" and "green" values, > so i want color to be enum type.. > > public enum ColorVal { > > BLUE("blue","blue"), > RED("red",red), > GREEN("green","green"), > > final String name; > > private ColorVal (String name) { > this.name = name > } > > String toString() { name } > } > > //domain class > Class ColorVal { > > .... > ColortVal color > > } > > In ColortValController i have "new" action, which saves domain class's > object in db... > > When i run .../myApp/ColorVal/new?color=blue, > > I get exception: > Error 500: ...: Cannot cast object 'PAYPAL' with class 'java.lang.String' to > class 'pack.ex.ColorVal' > > Please help me to understand my mistake.. > > > > > > > -- > View this message in context: http://grails.1312388.n4.nabble.com/enum-type-field-tp3337492p3337492.html > Sent from the Grails - user mailing list archive at Nabble.com. > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by hbbg
Your form needs to submit a value that matches the enum constants. It
looks like your form is submitting a lowercase value and your enum contstants are all uppercase. The name property you've added is kind of redundant. All enums have a name() method that return the String representation of the constant ('BLUE', 'GREEN', etc.) and a valueOf static method that does the conversion the other way. The valueOf method is what Grails uses to bind the form value to the enum property. If you really want to do binding a different way you can register a custom PropertyEditor for your enum type. I don't think there's any advantage to this, though (I presume you're not getting the user to enter enum values manually). --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
I have made some changes..
Now I in src/groovy myPack.ex I have: public enum ColorVal { COLOR1("color1"), COLOR2("color2") final String value ColorVal(String value) { this.value = value } public String value(){return value} } class ColorDomain{ ColorVal colorVal //.. } Now from controller I can get ColorVal value: ColorVal.COLOR1.value(), but when I enter in my browser myApp/myController/createAction?ColorVal = color1, where createAction have to save domain class fields in db. I get exception: Cannot cast object 'color1' with class 'java.lang.String' to class ' myPack.ex.ColorVal' I know that the exception is logical, but now I have not got view page .. I have to get ColorVal value like above.. |
|
put on your enum : String getKey() { value() } and in your gsp: <g:select name="colorVal" from="${myPack.ColorVal.values()}" value="${yourInstance?.colorVal?.key}" optionKey="key" noSelection="['': '']" /> Searching the ml found dozens asking the same (me included). :) On Mar 8, 2011, at 2:36 AM, hbbg wrote: I have made some changes.. |
|
Thank you for replying...
Now I have not got View page..is there any way to get enum value as a String? |
|
In reply to this post by wilsonaikeda@gmail.com
My problem is now in that I can't save Enum type value in my db..
|
|
Is there any specific place to find current documentation on how to use enum's in Grails and the ways GORM treats them? I have found various pieces around via Google, but it is a little hard to tell what is current.
On Thu, Mar 10, 2011 at 6:08 AM, hbbg <[hidden email]> wrote: My problem is now in that I can't save Enum type value in my db.. |
| Powered by Nabble | Edit this page |
