user2975535 user2975535 - 22 days ago 11
Scala Question

Play Framework how to set selected option in the form?

I need to set selected option for select element, rendered with Play Framework helper.
In the docs https://www.playframework.com/documentation/2.5.x/api/scala/index.html#views.html.helper.select$ I didn't find example how to do it.
I tried to do it like this, but still default value is selected:

@select(
field = form("selectedCategory"),
options = categoriesSeq,
'_default -> "Choose One",
'_label -> "Select category",
'selected -> "1"
)


I think this should be possible to achieve somehow.

Answer

You may want to look at how use play forms.

In your code, what is the variable form?

It should be a play.api.data.Form instance, passed into your view, from your controller.

I hope this helps.

Rhys

Comments