如何在游戏框架中验证表单
How to validate a form in play framework
我想在填写 json object 后验证表格。
通过逐步抛出代码,我在 Post Object 中看到了正确的值,但如果标题少于 5 个字符,我将不会收到任何错误。为什么,有人有想法吗?
@Constraints.Required
@Constraints.MinLength(5)
private String title;
Post post = Json.fromJson(json, Post.class);
if(postForm.hasErrors()){
/*
example
*/
private FormFactory formFactory;
@Inject
YourContructor(FormFactory formFactory){
this.formFactory
}
@BodyParser.Of(value = BodyParser.Json.class)
public static Result create() {
JsonNode json = request().body().asJson();
Form<Post> post= formFactory.form(Post.class).bind(json);
if(post.hasErrors()){
return badRequest(post.errorsAsJson());
}
return ok(json);
}
我想在填写 json object 后验证表格。 通过逐步抛出代码,我在 Post Object 中看到了正确的值,但如果标题少于 5 个字符,我将不会收到任何错误。为什么,有人有想法吗?
@Constraints.Required
@Constraints.MinLength(5)
private String title;
Post post = Json.fromJson(json, Post.class);
if(postForm.hasErrors()){
/*
example
*/
private FormFactory formFactory;
@Inject
YourContructor(FormFactory formFactory){
this.formFactory
}
@BodyParser.Of(value = BodyParser.Json.class)
public static Result create() {
JsonNode json = request().body().asJson();
Form<Post> post= formFactory.form(Post.class).bind(json);
if(post.hasErrors()){
return badRequest(post.errorsAsJson());
}
return ok(json);
}