I’m trying to follow Play!’s documentation for validation. I have a controller with 3 actions:
- add() – an “add website” form
- added() – called when the add form is submitted, does the actual adding.
- edit() – a successful added() call leads to editing the added website (a failed added leads back to the add() form).
When validation fails, I am redirected to the add() action as planned. The #{ifErrors} template works (<h1>Oops…</h1> is displayed) … but there are two problems:
- The website URL parameter is not “flashed” – in the add form, it doesn’t appear in the appropriate field.
- Specific errors do not appear – this field remains empty:
<span class="error">#{error 'website.url' /}</span>
What am I missing?
My Controller:
public class Sites extends Controller {
private static final WebsiteRepository websiteRepo = new WebsiteRepository();
public static void add() {
render();
}
public static void added(@Valid Website website) {
if (Validation.hasErrors()) {
Validation.keep();
params.flash();
add();
}
websiteRepo.save(website);
edit(website.id);
}
public static void edit(long websiteId) {
Website website = websiteRepo.getById(websiteId);
render(website);
}
}
add.html
<div id="addsite">
<h1>Add a new site</h1>
#{ifErrors}
<h1>Oops…</h1>
#{/ifErrors}
#{form @added()}
#{if flash.error}
<p class="error">
&{flash.error}
</p>
#{/if}
#{if flash.success}
<p class="success">
&{flash.success}
</p>
#{/if}
<p id="url-field">
<label for="url">URL</label>
<input type="text" name="website.url" id="url" class="url-field" value="${flash.url}" />
<span class="error">#{error 'website.url' /}</span>
</p>
<p id="add-field">
<input type="submit" id="add" value="Add" />
</p>
#{/form}
</div>
The value format is wrong, for complex objects use:
Edit: I cloned your repo and you have the following errors: