gpt4 book ai didi

java - 是否可以返回反馈错误,但仍允许 wicket 更新组件模型?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:10:56 24 4
gpt4 key购买 nike

我有一个 wicket 表单,其中包含许多 TextField 输入组件。大多数这些输入都有一个 validator 。

假设我输入了 50 个值,其中一个未通过范围 validator 。 Wicket 然后生成错误反馈消息,但也不会更新与每个组件关联的模型。结果是我丢失了刚刚输入的所有 50 个值,必须重新输入。

我的问题是,我可以告诉 Wicket 更新那些具有有效值的组件的模型,但只报告错误值的错误吗?

在框架中挖掘,我注意到 FormComponent 中的这段代码片段,这似乎表明如果有错误,则不要更新模型。

public final void processInput()
{
inputChanged();
validate();
if (hasErrorMessage())
{
invalid();
}
else
{
valid();
updateModel();
}
}

有什么方法可以自定义此行为,并实现我保留所有有效值的目标吗?

最佳答案

我敢打赌 FormComponent.processInput() 根本不会在这里被调用。当您提交 Form 时,Form.process()被调用。在那里,它将调用 Form.validate() ,这又会调用 Form.validateComponents() ,最终使用 FormComponent.validate()

您在这里遇到的问题是 Form.process() 中的全局处理。表单完全提交,或者根本不提交。当 FormComponent.validate() 失败时,Form.hasError() 将返回 true,因此 Form.process() 永远不会更新任何模型。

你可以:

  • 将所有 FormComponent 独立验证移至 FormValidator。您可以在那里选择更新那些通过验证的 FormComponents 的模型。
  • 实现 Form.onError() 并在那里使用访问者更新有效 FormComponents 的模型。
  • 重写 Form.process() 并修改 // If a validation error occurred分支使用您自己的方法将组件标记为有效/无效,并更新(或不更新)模型对象,具体取决于 FormComponent 是否有错误。 FormComponent.hasErrorMessage() 会告诉您某个 FormComponent 的验证是否失败。

更新

在与 OP 讨论了用户输入丢失的原因后,发现 FormComponents 被添加到没有 setReuseItems 的 ListView 设置为 true。这导致 FormComponents 在每个 ListView.populateItem() 上重新创建,因此丢失所有用户输入。

可以找到有关此问题性质的更多信息 here :

There are however a few provisions you have to take care of when using a repeater in the form. Usually repeaters clear out their items at the beginning of every request, when inside a form this is usually undesirable because you want to preserve the old items because you want them to keep their state as opposed to being recreated fresh.

For example, if you use ListView, you should call ListView.setReuseItems(true) inside the form so that it preserves old items instead of always creating new ones everytime.

关于java - 是否可以返回反馈错误,但仍允许 wicket 更新组件模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7979707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com