Multiselect select - Play 2.0?

I'm having big problems getting Multiselect to work with my Play 2.0 application.

I tried different solutions that I found on google, but no one works for 2.0.1.

Do you have any guides or tips for working with multiple favorites?

html ...

<select multiselect="multiselect" ... name="groupIds[]"> ... </select> 

The form

 class UserAdminForm{ public Long[] groupIds; } 

and then in the request handler ...

 Form<UserAdminForm> form = uform.bindFromRequest(); // Bam , [NumberFormatException: For input string: ""] 

Is there a good way to handle a POST array?

+4
source share
4 answers

I had the same problem, I thing multiselect form helper is a bug in Play 2. In any case, I fixed it by renaming select as @name []. This way you create a template for ex. selectMultiple.scala.html containing this code:

 @(field: play.api.data.Field, options: Seq[(String,String)], args: (Symbol,Any)*)(implicit handler: helper.FieldConstructor, lang: play.api.i18n.Lang) @values = @{ field.indexes.map { v => field("[" + v + "]").value } } @helper.input(field, args:_*) { (id, name, value, htmlArgs) => <select id="@id" name="@(name)[]" @toHtmlArgs(htmlArgs) multiple="multiple"> @args.toMap.get('_default).map { defaultValue => <option class="blank" value="">@defaultValue</option> } @options.map { v => <option value="@v._1" @{if(values.contains(Some(v._1))) "selected" else ""} >@v._2</option> } </select> } 

Having a list in your model for mapping the component, you use this template on your html page, for example:

 @selectMultiple( myForm("groupsId"), myOptions, '_label -> "My MultiSelect" ) 

Hoping this helps you! (note that I am using Play for Scala)

+3
source

You can create a template like the one below:

 @(field: play.api.data.Field, options: Seq[(String,String)], args: (Symbol,Any)*) (implicit handler: FieldConstructor, lang: play.api.i18n.Lang) @values = @{ field.indexes.map { v => field("[" + v + "]").value } } @input(field, args:_*) { (id, name, value, htmlArgs) => <select id="@id" name="@name" @toHtmlArgs(htmlArgs) multiple="multiple"> @options.map { v => <option value="@v._1" @{if(values.contains(Some(v._1))) "selected" else ""}>@v._2</option> } </select> } 

This example can be found in the play-framework discussion group.

+1
source

You are wrong:

 NumberFormatException: For input string: "" 

This means that you get an empty string that cannot be converted to a number. It is better to mark the field in the Form as optional, if it is possible not to get the value in this field.

0
source

Please see my example below game 2.2

Multiple-select dropdown options are in HashMap data

 @(data:HashMap[String, HashMap[String,String]]) @import helper._ @select(field = myform("options"), options = data.get("options").toSeq, '_label -> "My Options*", '_showConstraints -> false, 'class ->"required", 'id->"options", 'multiple->"multiple") 
0
source

Source: https://habr.com/ru/post/1411342/


All Articles