简体   繁体   English

验证不适用于Play Framework 2中的嵌套对象

[英]Validation Not Working on Nested Objects in Play Framework 2

I'm using the Constraint annotations for validating my objects in Play! 我正在使用Constraint注释来验证Play中的对象! framework 2. It works fine for top level objects, but not nested ones. 框架2.它适用于顶级对象,但不适用于嵌套对象。 How do I get my nested object validation annotations to work? 如何使嵌套对象验证注释生效?

Event: The top level object I am saving. 事件:我正在保存的顶级对象。

@Entity
public class Event {

    @Required
    public String name;

    @OneToMany(cascade = CascadeType.ALL)
    public List<Option> options;

    ...
}

Option: The nested object. 选项:嵌套对象。 Its not validating the name property. 它没有验证名称属性。

@Entity
public class Option {

    @Required
    public String name;

    ...
}

Not familiar with Play, but it looks very close to javax.validation, in which you'd need to put @Valid on your options field to tell the validator to descend into that relationship. 不熟悉Play,但它看起来非常接近javax.validation,你需要在你的options字段上放置@Valid来告诉验证者进入该关系。 Play has an @Valid of its own, so I'd give that a shot. Play有自己的@Valid ,所以我会给它一个镜头。

Update: As OP pointed out, the above is Play 1.0. 更新:正如OP所指出的,上面是Play 1.0。 Instead, per the Play 2.0 docs , it uses Spring data binding and JSR-303 under the hood, so how about using the javax.validation @Valid annotation on it? 相反,根据Play 2.0文档 ,它使用Spring数据绑定和JSR-303,那么如何使用javax.validation @Valid注释呢? Maybe Play 1.0 had a home-made validation framework and they decided to move to javax.validation in 2.0? 也许Play 1.0有一个自制的验证框架,他们决定转向2.0中的javax.validation? That would explain why several validation annotations seem to have disappeared in the 2.0 API. 这可以解释为什么几个验证注释似乎在2.0 API中消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM