|
This is a problem so straightforward I can't understand why I haven't come across it before. I am using an enum to record active status for an Organization domain object.
enum ActiveStatus {
ACTIVE("Active"),
NON_ACTIVE("Not Active")
String status
String toString() {
return status
}
ActiveStatus(String status) {
this.status = status
}
}
My Organization has an ActiveStatus property called 'status'. When my HTML form is submitted, I receive a 'status' param, such as 'Not Active' - i.e., the 'toString()' output of the ActiveStatus selected. But this causes a binding error: [Failed to convert property value of type 'java.lang.String' to required type 'com.myapp.ActiveStatus' for property 'status'] Hardly surprising, as I am indeed trying to bind a string to an ActiveStatus property - I just hoped that Grails would handle this conversion automatically for me. So what do I need to do to get the binding to happen automatically? Is there some way i could rewrite the enum? Or do I have to register a customer property editor? |
|
Let's see your enum definition.
-- Jonathan Rosenberg Founder & Executive Director Tabby's Place, a Cat Sanctuary http://www.tabbysplace.org/ On Fri, Jun 29, 2012 at 11:58 AM, John Moore <[hidden email]> wrote: > This is a problem so straightforward I can't understand why I haven't come > across it before. I am using an enum to record active status for an > Organization domain object. > > > > My Organization has an ActiveStatus property called 'status'. When my HTML > form is submitted, I receive a 'status' param, such as 'Not Active' - i.e., > the 'toString()' output of the ActiveStatus selected. But this causes a > binding error: > > [Failed to convert property value of type 'java.lang.String' to required > type 'com.myapp.ActiveStatus' for property 'status'] > > Hardly surprising, as I am indeed trying to bind a string to an ActiveStatus > property - I just hoped that Grails would handle this conversion > automatically for me. So what do I need to do to get the binding to happen > automatically? Is there some way i could rewrite the enum? Or do I have to > register a customer property editor? > > -- > View this message in context: http://grails.1312388.n4.nabble.com/Parameter-binding-with-enums-tp4630869.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 |
Hmm, that's a bit odd. I submitted the post via Nabble, because I have a recurrent problem with the email version of the list, and it appears fine there. But by email it's entirely stripped out the enum code from the middle. Here it is again, without the markup - hope it's visible this time. enum ActiveStatus { ACTIVE("Active"), NON_ACTIVE("Not Active") String status String toString() { return status } ActiveStatus(String status) { this.status = status } } |
|
Be sure that your select box (or whatever your using to populate the param is using ActiveStatus.name() and not ActiveStatus.toString() as it's value. This is working for me in 2.0.3.
Nathan Wells On Fri, Jun 29, 2012 at 10:13 AM, John Moore <[hidden email]> wrote:
|
|
When you say it's working for you, do you mean without any special binding technique, e.g. a custom property editor?
|
|
In reply to this post by Nathan Wells
I can see how that would work. My problem is that I'm using standard text input fields (out of necessity - I'm working with legacy HTML), which means that what is visible to the user, the input's value, is the same as what is submitted. So inevitably the parameter will contain the toString form, e.g., 'Not Active'), instead of the name() form ('NON_ACTIVE'). I wonder whether I should do something tricksy like the Grails checkbox tag does, and submit an accompanying hidden field, prefixed with an underscore. |
|
Well, if you can't modify from a textbox, I would manually modify the params before binding, rather than messing with hidden form elements. This is personal preference though, and I could imagine cases where you might want to use the hidden form elements.
This is assuming you can't change the HTML from a text input to a select input, which is somewhat... daft, as you blokes might say :) Nathan Wells On Fri, Jun 29, 2012 at 11:08 AM, John Moore <[hidden email]> wrote:
|
|
In reply to this post by John Moore
If that is the method you have to take (ie toString() value is submitted
instead of the name()), then you will most likely need to register a custom property editor. This is actually quite simple though as I remember it from a few months ago. It works great for me. Brian -----Original Message----- From: John Moore [mailto:[hidden email]] Sent: Friday, June 29, 2012 11:09 AM To: [hidden email] Subject: [grails-user] Re: Parameter binding with enums? Nathan Wells wrote > > Be sure that your select box (or whatever your using to populate the > param is using ActiveStatus.name() and not ActiveStatus.toString() as > it's value. > I can see how that would work. My problem is that I'm using standard text input fields (out of necessity - I'm working with legacy HTML), which means that what is visible to the user, the input's value, is the same as what is submitted. So inevitably the parameter will contain the toString form, e.g., 'Not Active'), instead of the name() form ('NON_ACTIVE'). I wonder whether I should do something tricksy like the Grails checkbox tag does, and submit an accompanying hidden field, prefixed with an underscore. -- View this message in context: http://grails.1312388.n4.nabble.com/Parameter-binding-with-enums-tp4630869 p4630881.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 |
|
That's the approach I'll take, then. Thanks!
|
| Powered by Nabble | Edit this page |
