Skip to content

Instantly share code, notes, and snippets.

@janetruluck
Created February 18, 2014 03:29
Show Gist options
  • Save janetruluck/9064190 to your computer and use it in GitHub Desktop.
Save janetruluck/9064190 to your computer and use it in GitHub Desktop.
Play! framework form validations
package controllers;
// Java
import java.util.*;
// Play
import play.*;
import play.mvc.*;
import play.libs.Json;
import play.data.validation.Constraints;
import play.data.Form;
import models.*;
// JSON
import com.fasterxml.jackson.databind.JsonNode;
public class Users extends Controller {
public static Result newUser() {
Form<UserCreate> userCreateForm = Form.form(UserCreate.class);
userCreateForm = userCreateForm.bind(request().body().asJson());
if (userCreateForm.hasErrors()) {
return badRequest(userCreateForm.errorsAsJson());
} else {
UserCreate userCreate = userCreateForm.bindFromRequest().get();
User user = User.create(userCreate.email);
return created(Json.toJson(user));
}
}
public static class UserCreate {
@Constraints.Required
public String email;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment