Skip to content

Instantly share code, notes, and snippets.

@knicklabs
Created January 11, 2012 13:07
Show Gist options
  • Save knicklabs/1594575 to your computer and use it in GitHub Desktop.
Save knicklabs/1594575 to your computer and use it in GitHub Desktop.
Model-Level Validations in Play Framework 1.x
// This example was summarized from http://www.playframework.org/documentation/1.2/validation and
// reproduced here for quick reference.
// To achieve model-level validations (versus handling at controller level), first annotate
// the properties of the model.
package models;
public class User {
@Required
public String name;
@Required
@Min(0)
public Integer age;
}
// Next, in the controller, specify that all properties must be valid.
public static void hello(@Valid User user) {
if(validation.hasErrors()) {
params.flash(); // add http parameters to the flash scope
validation.keep(); // keep the errors for the next request
index();
}
render(name, age);
}
// And the modified form...
#{ifErrors}
<h1>Oops…</h1>
#{/ifErrors}
#{form @Application.hello()}
<div>
Name: <input type="text" name="user.name" value="${flash['user.name']}" />
<span class="error">#{error 'user.name' /}</span>
</div>
<div>
Age: <input type="text" name="user.age" value="${flash['user.age']}" />
<span class="error">#{error 'user.age' /}</span>
</div>
<div>
<input type="submit" value="Say hello" />
</div>
#{/form}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment