title: play framework: how to repopulate form on validation-failure when using datamodel?
body: <p>I'm building some crude CMS-like functionality (to get acquinted with Play Framework). For this test-case I've build 2 pages, 1 for listing tags and 1 for creating/editing/saving tags. </p>
  
  <p>The flow is like this (routes-file): </p>
  
  <pre><code>#list tags
  GET /tags  Application.listTags 
  
  #view/edit existing tag
  GET /tag/{&lt;(?!new$)(.+)&gt;name} Application.showTag  
  
  #new tag
  GET /tag/new  Application.showTag   
  </code></pre>
  
  <p>the create/view/edit page displays a form which gets it's values from a tagDTO. 
  The normal flow works without problems, but when the form gives validation-errors (e.g: the tag-name must exist) I want to display the page again, repopulating the form with the edited values. </p>
  
  <p>For this (following the Play Framework conventions) I could use the 'flash'-object which contains these last values, but the form is already bound to the tagDTO (which is null on redirect) instead of the 'flash'-object. </p>
  
  <p>First the code: 
  Application.java</p>
  
  <pre><code>.....
  
  public static void showTag(String name) {
      TagDTO tagDTO = TagDTO.buildDTOFromModelOrNew(name);
      render(tagDTO); 
  }
  
  
  /**
   * Save tag and redirect to Show
   * 
   * @param name
   * @param displayname
   * @param isnew
   */
  public static void saveTag(
          @Required(message="Name is required") String name,
          String displayname,
          boolean isnew) 
  {
      checkAuthenticity();
      if(validation.hasErrors()) {
          params.flash(); 
          validation.keep(); 
          showTag(null);
      }
  
      //fetch tagDTO based on backend or create new if not exist
      TagDTO tag = TagDTO.buildDTOFromModelOrNew(name);
  
      // Append / Overwrite values
      tag.displayname = displayname;
      tag.name = name;
  
      //save result to model
      TagDTO.buildAndSaveModelFromDTO(tag);
  
      flash.success("Thanks for " + (isnew?"creating":"updating") + " tag " + tag.name);
  
      //redirect to show
      showTag(tag.name);
  } 
  </code></pre>
  
  <p>And ShowTag.html</p>
  
  <pre><code>    #{extends 'main.html' /}
      #{if flash.success}
          &lt;p class="success"&gt;${flash.success}&lt;/p&gt;
      #{/if}
  
      #{ifErrors}
          &lt;p class="errors"&gt;Oops...&lt;/p&gt;
      #{/ifErrors}
  
      #{form @Application.saveTag()}
          #{authenticityToken /}
          &lt;p&gt;
              &lt;label for="name"&gt;Name: &lt;/label&gt;
              &lt;input type="text" name="name" id="name" value="${tagDTO.name}" /&gt;
              &lt;span class="error"&gt;#{error 'name' /}&lt;/span&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;label for="displayname"&gt;Displayname: &lt;/label&gt;
              &lt;input type="text" name="displayname" id="displayname" value="${tagDTO.displayname}" /&gt;
              &lt;span class="error"&gt;#{error 'displayname' /}&lt;/span&gt; 
          &lt;/p&gt;
          &lt;p&gt;
              &lt;input type="hidden" name="isnew" value="${tagDTO.isnew}" /&gt;
              &lt;input type="submit" value="Submit your comment" /&gt;
          &lt;/p&gt;
      #{/form}
  </code></pre>
  
  <p>Now I could think of some ways to make it work, but none really elegant: </p>
  
  <ol>
  <li><p>bind the form to the flash-object (or params-object) and populate the flas/params- object from the tagDTO</p></li>
  <li><p>on validation-failure, refetch the tagDTO (not avail anymore so DB-call necessary) and overwrite values in tagDTO with values available in flash-object, bind form to tagDTO. </p></li>
  <li><p>like 2, but using some sort of cache to quickly fetch tagDTO (so no need for db-call)</p></li>
  <li><p>Some general  mechanism to (de)serialize tagDTO from/to the session.</p></li>
  </ol>
  
  <p>In short, I don't like any of them really. 
  What would you consider to be a best practice in this situation? Or is there any functionality in the Play Framework that I'm missing? </p>