Struts 2:使用 ValidationInterceptor 验证子集合游客

发布于 2024-10-11 16:57:25 字数 516 浏览 6 评论 0原文

我有一个模型对象,它具有标题-详细信息关系中的子级集合,标题在一个操作类(和表单)中处理,详细信息在单独的操作类(和表单)中编辑。我希望在保存标头对象时能够对标头中的整个对象图进行完整验证;我已经在标头的 -validation.xml 中声明了一个访问者:(我们在此项目中使用 XML 验证)

<field name="details">
    <field-validator type="visitor" />
</field>

详细信息类也具有适当的验证。

我们碰巧还从另一个图中创建/预填充了这个特定的对象图;然而,预填充会将一些例如必填字段保留为空。

发生的情况是,只有标头得到验证,因为这是在 save 方法运行时提交的表单。当实际上详细信息在标题表单中没有任何表单元素时,是否也可以强制验证详细信息(通过我声明的访问者)?

我想让它保持干燥,而不必在标头操作的 validate() 方法中进行显式验证。

I have a model object that has a collection of children in a header-detail relationship, with the header handled in one action class (and form) and the details edited in a separate action class (and form). I want to be able to do a full validation of the whole graph of objects from the header when I save the header object; I've already declared a visitor as such inside the header's -validation.xml: (we're using XML validation in this project)

<field name="details">
    <field-validator type="visitor" />
</field>

The detail class also has the proper validation.

We happen to also create/prepopulate this particular object graph from another graph; however, the prepopulation will leave some e.g. required fields as empty.

What happens is that only the header gets validated, since that's the form being submitted when the save method runs. Is it possible to force validation of the details as well (via the visitor I declared), when in fact the details don't have any form elements in the header form?

I want to keep it DRY and not have to do explicit validation inside the header action's validate() method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野の 2024-10-18 16:57:25

部分问题在于,当验证运行时,struts 只能看到从表单创建的对象。如果表单中未以某种方式引用详细信息列表,则 xml 验证将无法查看该信息以对其进行验证。

一种可能的解决方案是通过隐藏字段在表单中包含详细信息。您可以迭代列表,并为每个详细信息对象添加一个隐藏字段。您可能需要某种自定义数据转换器,除非您希望详细信息对象的每个数据成员都有一个隐藏字段。

除此之外,我看不到任何仅通过 xml 验证来完成此操作的方法。

Part of the problem is that when the validation runs, struts only sees the object as it was created from the form. If the list of details are not referenced in the form in some way, the xml validation won't be able to see that information in order to validate it.

One possible solution would be to include the detail information in the form through hidden fields. You could iterate over the list, and add a hidden field for each detail object. You would probably need some kind of custom data converter, unless you wanted a hidden field for each data member of the detail object.

Other than that, I can't see any way to do it solely through xml validation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文