Search code examples
playframeworkdynamicform

How can I set values of DynamicForm data in Play Framework


I'm trying to fetch previous values of a form from cache, clear one value and render the form with all the previous values except the one cleared.

DynamicForm dform = (DynamicForm) Cache.get("dform");
Cache.set("dform",null,0);
if (dform == null) {
  dform = new DynamicForm();
} else {
  dform.data().put("name","");
}
return ok(myform.render(dform));

Everything else works like expected except dform.data().put("name","") does not change the value of name field. Why? How can I set that field's value?


Solution

  • From play 2.1, you can't change data. If you want to change it, you need to do the following:

    Map<String,String> data = dform.data();
    data.put("name", "");
    dform = new DynamicForm().fill(data);
    

    Good luck.