Play 2.3.4 Java, select element of form: setting the chosen one

1,333 views
Skip to first unread message

John Gateley

unread,
Oct 18, 2014, 12:01:49 AM10/18/14
to play-fr...@googlegroups.com
Hi,

This is Play 2.3.4, with Java.

I have a form with a select element:

    @form(action = ...) {
        Select field:
        @helper.select(field = myForm("myField"), options(Seq("Foo", "Bar")))
...

How can I specify which element is selected when the form is first displayed?

This is a short example, but in a longer form, where the select field has many values to choose from, I need to be able to set the value that is displayed (like for handling errors).

Thanks

John

Mateusz Lisik

unread,
Oct 18, 2014, 8:06:14 AM10/18/14
to play-fr...@googlegroups.com

Pozdrawiam,
Mateusz Lisik

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Gateley

unread,
Oct 18, 2014, 10:29:37 AM10/18/14
to play-fr...@googlegroups.com
Thanks for the response.

Unfortunately, this does not respect the order of the <option> elements.
The default is more appropriate for a header like "Choose one of the cities below", but if you have cities in alphabetical order, the _default city is not in the proper order.
As a side note, the _defaul argument should not appear in the list of options, or it will show up twice.

Any other ideas?

Thanks again

John

jaybytez

unread,
Oct 24, 2014, 12:14:54 AM10/24/14
to play-fr...@googlegroups.com
You could just pre-populate your field in your form object with the default value if you are passing that into your template. 

jay

John Gateley

unread,
Oct 24, 2014, 11:50:49 PM10/24/14
to play-fr...@googlegroups.com
Hi Jay,

I've tried this, in a couple of different ways, but it doesn't seem to work. Likely that I'm doing something wrong, but I'm not sure what.

I've been using a for loop to generate the options, and passing the selected option in as a parameter:
        @for(value <- values) {
            <option value="@value" @if(value == selectedValue) { selected }>
            @value
            </option>
        }

This works, but isn't elegant.

John

Mateusz Lisik

unread,
Oct 25, 2014, 7:03:52 AM10/25/14
to play-fr...@googlegroups.com
Hi.

Select helper will add "selected" attribute to option that is set in your model. Recently I've had something like this:
        @helper.select(book("language"), options = helper.options(commons.LanguageHelper.getLanguages), args = '_showConstraints -> false,
            '_label -> "Language",
            'class -> "form-control",
            'placeholder -> "(Required) Select language of your book",
            'required -> true
        )

getLanguages returns Map<String, String> so if Map Key equals book.language then it will be marked as selected and wont be placed as first item.

Pozdrawiam,
Mateusz Lisik

John Gateley

unread,
Oct 25, 2014, 10:16:28 PM10/25/14
to play-fr...@googlegroups.com
Thank you Mateusz, that helped.

I needed options in order, so the map wouldn't work, but I found out that using helper.options with a list could do the same. Then the value in the form must be the option that appears in the list.


John

virtualeyes

unread,
Oct 27, 2014, 1:52:28 AM10/27/14
to play-fr...@googlegroups.com
To force a selected value you need to do something like:
field = form("foo").copy(value = form("foo").value.getOrElse(Some(/*default value*/)))

Kind of boilerplate-y but I believe that's the only way to pre-select an option element when a play form has no bound value for the target field (e.g. on create event).

p.s. if above doesn't compile, add yet more boilerplate ;-)
field = form("foo").copy(value = form("foo").value.map(Some(_)).getOrElse(Some(/*default value*/)))
Reply all
Reply to author
Forward
0 new messages