Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.