播放scala将List [Int]值从表单传递到控制器显示没有为类型List [Int]找到URL路径绑定并且类型不匹配错误

时间:2015-06-29 11:54:37

标签: list scala playframework playframework-2.3 scala-template

在我的Play scala project我有多个drop down个项目。我已成功查看scala.htmldrop down个文件,并在values(List[Int])中选择了多个值。

我需要什么

提交表单时,我需要将多个选定的下拉formcontroller传递到employeeForm("languagesids").value,为此我使用了[error] found : Option[String] [error] required: List[Int] 但它显示如下错误

employeeForm("languagesids").get

我尝试过使用 No URL path bind er found for type List[Int] ,但这也无济于事。 以及它的展示

Controller

路线文件中的此错误

我尝试了什么

我尝试通过以下代码访问def registerEmployee = Action { implicit request => algorithmForm.bindFromRequest.fold( formWithErrors => BadRequest(html.employeeRegisterForm(formWithErrors,LanguageType.options)), employee=> { play.api.Logger.info("employee langauge list size :"+employee.languagesids.size) 中的选定值列表

     val employeeForm= Form(
        mapping(
          "employeeid" -> ignored(None:Option[Int]),
          "employeename" -> nonEmptyText,    
          "languagesids" -> ignored(None:Option[List[Int]])

        )(models.Employee.apply)(models.Employee.unapply)
      )

 def createEmployee = Action {  implicit request =>      
       Ok(html.employeeRegisterForm(employeeForm,LanguageType.options.options))
       }

     }

如果我们选择项目

,它会将列表大小设为0

控制器/ Employees.scala

  case class Employee(
      employeeid:  Option[Int] = None,
      employeename:String,  
      languagesids:Option[List[Int]] = None
        )
    case class LanguageType(
     languageid: Option[Int] = None,
  languagename: String
  )
    object Employee{


    }
object LanguageType{

   val simple = {
    get[Option[Int]]("language_type.LANG_ID") ~
    get[String]("language_type.LANG_NAME") map {      
      case languageid~languagename=> LanguageType(languageid,languagename)
    }
  }

  def options: Seq[(String,String)] = DB.withConnection { implicit connection =>
 SQL("select * from language_type")      
      foldLeft[Seq[(String, String)]](Nil) { (cs, c) => 
        c.languageid.fold(cs) { id => cs :+ (id.toString -> c.languagename) }
      }
  } 

}

模型/ Employee.scala

@(employeeForm: Form[Employee],languageTypes: Seq[(String, String)])


    @form(routes.Employee.registerEmployee(employeeForm("languagesids").value) {

        <fieldset>

            @inputText((employeeForm("algoname"), '_label -> "Employee Name", '_help -> "",'placeholder -> "Employee Name")
            @inputText(employeeForm("algodesc"), '_label -> "Employee Description", '_help -> "",'placeholder -> "Description")      


             @multiSelect(
    employeeForm("languagesids"),
    languageTypes,
    '_label -> "Select Tags" ,'_default -> "-- Choose Tag --"         
  )         

        </fieldset>

        <div class="actions">
            <input type="submit" value="Create Employee" > 

        </div>

    }

}

视图/ employeeRegisterForm.scala.html

   GET  /register/:languageList     controllers.Employees.registerEmployee(languageList:List[Int])

路由

@**
 * Generate an HTML multi-select.
 *
 * Example:
 * {{{
 * @select(field = myForm("categories"), options = options("A","B","C"))
 * }}}
 *
 * @param field The form field.
 * @param args Set of extra attributes.
 * @param handler The field constructor.
 *@

 @import helper._

@(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>
}

视图/ multiSelect.scala.html

r

0 个答案:

没有答案