Reputation: 230038
I'm trying to follow Play!'s documentation for validation. I have a controller with 3 actions:
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:
<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>
Upvotes: 1
Views: 891
Reputation: 11274
The value format is wrong, for complex objects use:
${flash['website.url']}
Edit: I cloned your repo and you have the following errors:
Upvotes: 2
Reputation: 16439
Instead of calling add() try:
render("Sites/add.html", website);
where the first term is the path to the Add template and the second the form parameter you get in the POST call.
The first parameter can be referenced also as "@add" (shortcut)
Upvotes: 0