我正在使用以下类来验证表单提交,如下所述:https://www.playframework.com/documentation/2.3.4/JavaForms
public class ExampleForm {
@Required
public int height;
public String payload;
}
当我执行bindFromRequest()时,将忽略@Required约束。但是,当我将基元类型更改为盒装类型时,它可以工作:
public class ExampleForm {
@Required
public Integer height;
public String payload;
}
bean验证不适用于原始类型吗?
答案 0 :(得分:1)
说明很简单:数据库允许NULL值,而原始类型不允许。由于Form实际上是UI和DB模型之间的某种连接器,因此它需要对象而不是基元。
@Required
检查object
在第一步是否为null且int为非null。
保持规则,在模型和表单中使用Integer
代替int
,您的验证器将始终按预期工作。