Play 框架中的表单验证
Form Validation in Play framework
我正在 Java 中使用 Play 2.3 创建一个 Play 应用程序。
我正在尝试使用表单处理 POST 正文为 JSON 的请求。
我的问题是,如果我的 JSON 是一个只有 Strings 或 Floats 属性的简单对象,它就可以正常工作。但是,如果我放置一些对象叠层,它会继续正确地绑定请求,但不会在嵌套对象中进行约束验证。
这是我正在尝试做的一个例子:
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;
}
我错过了什么吗?这很奇怪,因为我认为它在第一次工作...但我不能确定。
不仅需要注释 @Required
,对于复杂对象还需要注释 @Valid
,正如此处指出的:Bind complex (JSON) form data automatically
我正在 Java 中使用 Play 2.3 创建一个 Play 应用程序。
我正在尝试使用表单处理 POST 正文为 JSON 的请求。
我的问题是,如果我的 JSON 是一个只有 Strings 或 Floats 属性的简单对象,它就可以正常工作。但是,如果我放置一些对象叠层,它会继续正确地绑定请求,但不会在嵌套对象中进行约束验证。
这是我正在尝试做的一个例子:
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;
}
我错过了什么吗?这很奇怪,因为我认为它在第一次工作...但我不能确定。
不仅需要注释 @Required
,对于复杂对象还需要注释 @Valid
,正如此处指出的:Bind complex (JSON) form data automatically