Play框架中的表单验证 [英] Form Validation in Play framework

查看:111
本文介绍了Play框架中的表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java中的Play 2.3创建一个Play应用程序.

I am creating a Play application with Play 2.3 in Java.

我正在尝试使用Form处理带有JSON正文的POST请求.

I'm trying to use Form for handling POST request with JSON body.

我的问题是,如果我的JSON是仅具有Strings或Floats属性的简单对象,则它会很好地工作.但是,如果我放一些对象约束,它将继续牢固地绑定请求,但不对嵌套对象进行约束验证.

My problem is that if my JSON is a simple object with only Strings or Floats attribute, it works well. But if I put some of Object imbrication, it continue to bind the request corectly but don't do the Constraints validation in nested objects.

以下是我要执行的操作的一个示例:

Here an exemple of what I'm trying to do :

public class PairRequest
{
    @Required
    public String epc;

    @Required
    public RequestProduct product;
}

public class RequestProduct
{
    //Product data
    @Constraints.Required
    private String productCode;

    @Constraints.Required
    public Brand brand;

    @Constraints.Required
    private String furniture;
}

@Entity
public class Brand extends Model {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    @Column(length = 250)
    @Constraints.Required
    public String name;

    @Column(nullable = true, length = 512)
    public String regex;
}

我错过了什么吗?这很奇怪,因为我认为它是第一次运行.但是我不确定.

Have I missed something ? It's weird because I think it was working during first times... But I can't be sure.

推荐答案

不仅需要注释@Required,而且还需要复杂对象的@Valid,如下所示:

Not only the annotation @Required is needed but also the @Valid for complex objects, as pointed out here: Bind complex (JSON) form data automatically

这篇关于Play框架中的表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆