Last active
August 29, 2015 14:05
-
-
Save rkaneko/f35ebe3adb38979e5f95 to your computer and use it in GitHub Desktop.
Play 2.0 Java Form validation sample.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import play.mvc.BodyParser; | |
import play.mvc.Controller; | |
import play.mvc.Result; | |
import play.data.Form; | |
import forms.CreateAccountForm; | |
public class Application extends Controller { | |
@BodyParser.Of(BodyParser.Json.class) | |
public static Result create() { | |
Form<CreateAccountForm> form = Form.form(CreateAccountForm.class) | |
.bind( | |
request().body().asJson(), | |
"user_id", | |
"password" | |
); | |
// check validation error | |
if (form.hasErrors()) { | |
System.out.println("validation.error: " + form.errors()); | |
return badRequest(); | |
} else { | |
System.out.println("valid json parameters"); | |
ok(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package forms; | |
import forms.validators.UserIdUniqueValidator; | |
import play.data.validation.Constraints; | |
public class CreateAccountForm { | |
@Constraints.Required(message = "必須項目です。") | |
@Constraints.MinLength(value = 3, message = "最低3文字必要です。") | |
@Constraints.MaxLength(value = 16, message = "16文字までです。") | |
@Constraints.Pattern(value = "^[a-zA-Z0-9\\._\\-]+$", message = "使用できる文字は半角英数字(大文字小文字)、_[アンダーバー]、-[ハイフン]です") | |
@Constraints.ValidateWith(UserIdUniqueValidator.class) | |
public String user_id; | |
@Constraints.Required(message = "必須項目です。") | |
@Constraints.MinLength(value = 3, message = "最低3文字必要です。") | |
@Constraints.MaxLength(value = 16, message = "16文字までです。") | |
@Constraints.Pattern(value = "^[a-zA-Z0-9\\.\\-_]+$", message = "使用できる文字は半角英数字(大文字小文字)、_[アンダーバー]、-[ハイフン]です") | |
@Constraints.ValidateWith(UserIdUniqueValidator.class) | |
public String password; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package forms.validators; | |
import models.User; | |
import play.data.validation.Constraints; | |
import play.libs.F; | |
import javax.validation.ConstraintValidator; | |
public class UserIdUniqueValidator | |
extends Constraints.Validator<String> | |
{ | |
@Override | |
public boolean isValid(String o) { | |
return User.isIdUnique(o); | |
} | |
@Override | |
public F.Tuple<String, Object[]> getErrorMessageKey() { | |
return F.Tuple("既に使用されています。", new Object[] {}); | |
} | |
public static Constraints.Validator<String> useridunique() { | |
return new UserIdUniqueValidator(); | |
} | |
} |
TODO
- 入れ子構造になるJSONパラメータの時は、keyがかぶった場合はどうなるか?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References