如何强制Wicket“更改"?如果字段未通过验证条件,则将触发AJAX事件 [英] How to force Wicket "onchange" AJAX events to be triggered if fields fail validation conditions

查看:44
本文介绍了如何强制Wicket“更改"?如果字段未通过验证条件,则将触发AJAX事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想到的具体情况如下:将AjaxFormComponentUpdatingBehavior("onchange")添加到表单的TextField中.行为验证文本是否符合特定条件(无关紧要的是模型对象还是表单组件模型),并以此为依据可能显示消息(或隐藏消息)(如果已经显示的话).

The specific case I've got in mind is as follows: an AjaxFormComponentUpdatingBehavior("onchange") is added to a TextField in a form. The behavior verifies the text for certain conditions (either the model object or the form component model, doesn't matter), based on which it might display a message (or hide it, if it has already been shown).

问题是,还向文本字段添加了验证器.可能的(和可能的)情况之一包括用户首先键入一个值,该值使AJAX请求显示消息.然后,如果他/她输入了未通过验证的,则消息应消失,但不会消失.

The problem is, there are also validators added to the TextField. One of the possible (and likely) scenarios consists of the user typing in, first, a value that causes the message to be displayed by the AJAX request. If, then, he/she types in a value that doesn't pass validation, the message should disappear, but it does not.

显然,根本没有调用AJAX行为的 onUpdate()方法,或者我尝试插入检查未验证的条目失败了>(我尝试过测试null值和空字符串,都无济于事;我不知道当数据无效时Wicket的验证器对模型做了什么).

Apparently, either the onUpdate() method for the AJAX behavior is not called at all, or I am failing in my attempts to insert a check for non-validated entries (I have tried to test for both null values and empty strings, to no avail; I have no idea what exactly Wicket's validators do to models when data is invalid).

我想知道真正了解验证器(或实际上是AJAX)的人对问题可能出在哪里有任何想法.

I am wondering if someone who actually understands validators (or AJAX, actually) has any ideas on where the problem could be.

如果有人告诉我这不是将验证器和AJAX捆绑在一起的普遍问题,而是很可能是编程错误,那么我可以发布编辑和邮政编码.我仍然相信前者,因此,我将避免发布代码部分,以使讨论保持在API/理论框架上.

I can post edit and post code if someone tells me this is not a general issue tying validators and AJAX, but most likely a programming mistake. I still believe the former and thus I'll refrain from posting code sections, in order to keep the discussion on an API/theoretical frame.

谢谢.

推荐答案

在使用 AjaxFormComponentUpdatingBehavior 时,如果任何 IValidator 均未通过验证,则 onError() 将被调用,而不是

When using an AjaxFormComponentUpdatingBehavior, if any of the IValidators fail their validation, onError() will be called instead of onUpdate(). Wicket will effectively prevent invalid user input from reaching the IModels in your components, so the component's ModelObject will not be changed at all. The invalid input will probably remain available by means of getInput()/getConvertedInput() (not sure if it will in an AJAX scenario, it sure is in a traditional form submission).

但是,请考虑到使用此机制时不会执行 IFormValidator .如果有的话,您可能有兴趣覆盖

However, take into account that IFormValidators are not executed when using this mechanism. If you've got any, you might be interested in overriding getUpdateModel() so that AjaxFormComponentUpdatingBehavior will not bring maybe-invalid user input into your IModels, and set modelobjects manually when you're certain user input is valid.

关于您的特定情况,您可以在 onError()中执行所有必需的逻辑(或依靠从其他地方获取数据的 Model s),将需要刷新的组件添加到 AjaxRequestTarget 中.这可能是您的方案中所缺少的.

Regarding your specific case, you could perform all the required logic in onError() (or rely on Models that will grab data from somewhere else), and just add the components that need refreshing to the AjaxRequestTarget. This is probably what's missing in your scenario.

这篇关于如何强制Wicket“更改"?如果字段未通过验证条件,则将触发AJAX事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆