如果验证失败,则播放框架复选框列表为空

时间:2015-06-15 09:46:31

标签: validation playframework checkboxlist

我有一个带有文本字段和复选框列表的简单表单。我注意到如果文本字段的值无效,则复选框列表为空。这是一个例子:

表格

public class UserForm {
    @Required
    private String name;
    private List<UserTag> tags; //UserTag is an enum
}

视图

@form(action = routes.UserController.create(), 'role -> "form") {
<input type="text" name="name" class="form-control" value="@userCreateForm("name").value">
<ul>
@for(i <- 0 to (UserTag.values.length - 1)){
<li><input type="checkbox" value="@UserTag.values[i]" name="tags[i]"> @Messages(...)</label></li>
}
</ul>    
}

堆栈跟踪

    play.api.Application$$anon$1: Execution exception[[NullPointerException: null]]
    at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.7.jar:2.3.7]
    at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.7.jar:2.3.7]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.7.jar:2.3.7]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.7.jar:2.3.7]
    at scala.Option.map(Option.scala:145) [scala-library-2.11.1.jar:na]
Caused by: java.lang.NullPointerException: null
    at scala.collection.immutable.StringOps$.length$extension(StringOps.scala:48) ~[scala-library-2.11.1.jar:na]
    at scala.collection.immutable.StringOps.length(StringOps.scala:48) ~[scala-library-2.11.1.jar:na]
    at scala.collection.IndexedSeqOptimized$class.prefixLengthImpl(IndexedSeqOptimized.scala:38) ~[scala-library-2.11.1.jar:na]
    at scala.collection.IndexedSeqOptimized$class.exists(IndexedSeqOptimized.scala:46) ~[scala-library-2.11.1.jar:na]
    at scala.collection.immutable.StringOps.exists(StringOps.scala:30) ~[scala-library-2.11.1.jar:na]

问题是id用户提交了一个带有“name”的表单为空和一些标签,我想向用户显示所选标签,但我得到一个空指针异常。

0 个答案:

没有答案