Cyril N.
Cyril N.

Reputation: 39889

form().bindFromRequest() with array values

I have a <select name="items" multiple> in the front end, and I'm using form().bindFromRequest() in the backend, then using a DynamicForm.

But here's the hiccup:

DynamicForm form = form().bindFromRequest();
form.field("items").value(); // Only return one value !

How can I get all the submitted value? without having to go through request().body().asFormUrlEncoded().get("items") if possible.

Upvotes: 5

Views: 2555

Answers (1)

Pawel Hawro
Pawel Hawro

Reputation: 1536

Binding array values require to properly name value in request. Param name should end with "[]" to be bound as an array (List) value.

In your HTML you should have:

<select name="items[]" multiple>

In Form class add it as:

public class ExampleForm {
    public List<Integer> items;
}

I know this not solve DynamicForm problem, but it is far more elegant solution.

Upvotes: 4

Related Questions