play framework checkbox list is null if validation fails

156 views Asked by At

I've a simple form with a text field and a list of checkboxes. I noted that if the value of the text field is invalid then the list of checkboxes is null. Here an example:

Form

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

View

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

Stack Trace

    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]

The problem is that id the user submit a form with "name" empty and some tags, I'd like to show the user the selected tags, but I get a null pointer exception.

0

There are 0 answers