Grails g:select no selection
Asked Answered
C

3

8

I have the following combobox:

<g:select name="ticketType" from="${app.domain.enums.TicketType?.values()}"
                              keys="${app.domain.enums.TicketType.values() }"
                              value="${ticketInstance?.ticketType}"
                              noSelection="${['null': 'Select One...']}"
                    />

I've setup the following constraint for ticketType in command object

ticketType nullable: true, blank:true

TicketType is a very simple enum:

    public enum TicketType {
        QUESTION, SUPPORT, MAINTENANCE, NEW_FUNCTIONALITY, MALFUNCTION
}

And every time I don't setup some value for ticketType in my GSP I get the following error:

Failed to convert property value of type 'java.lang.String' to required type 'com.coming.enums.TicketPriority'

It's like in case of no selection g:select sets the value for "null" (string).

What am I missing?

Conker answered 26/7, 2012 at 12:41 Comment(2)
Please show the code of your g:select ...Raspy
@Raspy Sorry - there was some error in formatting. I've edited my post.Conker
S
17

Rather than using the 'null' literal, have you tried using an empty string as your noSelection attribute? e.g. noSelection="${['':'Select One...']}"? This may do a proper conversion to a true null value during data binding.

Snoopy answered 26/7, 2012 at 14:32 Comment(1)
That's how I usually do it but it didn't work. So I played with different options. In the mean time I've changed some code in controllers and services, and return noSelection to noSelection="${['':'Select One...']}" and now it works. Gremlins :)Conker
R
2

As your error says - you do have a string in your noSelection. This can't be converted to any of your enum values.

Remove the quotation marks of your null and it should work (it works for me with grails 2.0):

<g:select name="ticketType" from="${app.domain.enums.TicketType?.values()}"
                          keys="${app.domain.enums.TicketType.values() }"
                          value="${ticketInstance?.ticketType}"
                          noSelection="${[null: 'Select One...']}"/>
Raspy answered 26/7, 2012 at 13:15 Comment(2)
I've tried that allready and it's still not working. Any other ideas?Conker
g:select ends up producing HTML, so it doesn't make any difference the quotations around null, since it will produce value="null" anyway...Kaikaia
I
0

Under Rails 3.3.x, no variation of providing the "null" value worked. In the end, I resolved it by adding this line in to the controller+action before doing any use of the params:

// convert "null" strings to REAL nulls
params.account.each { it.value = (it.value == 'null' ? null : it.value) }

After that, the following worked fine:

account.properties = params.account
Imputation answered 17/3, 2021 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.