AjaxFormComponentUpdatingBehavior和表单提交的不同验证器集 [英] Different set of validators for AjaxFormComponentUpdatingBehavior and form submission

查看:58
本文介绍了AjaxFormComponentUpdatingBehavior和表单提交的不同验证器集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Wicket中,我编写了一个AjaxFeedbackComponent组件,该组件采用常规表单组件(例如TextField)和反馈字段,并在用户键入时提供带有即时反馈的验证.我使用它进行用户注册,例如,验证最小密码长度或重复的用户名等(类似于Twitter注册页面).为此,我已经使用表单组件注册了 AjaxFormComponentUpdatingBehavior ,并使用ajax请求更新了反馈字段:

In Wicket, I have written a AjaxFeedbackComponent component that takes a regular form component (such as a TextField) and feedback field and provides validation with instant feedback as the user types. I use it for user registration, for example, to validate the minimum password length, or a duplicate username etc (similar to the Twitter signup page). For, I have registered a AjaxFormComponentUpdatingBehavior with the form component and update the feedback field with the ajax request:

formComponent.add(new AjaxFormComponentUpdatingBehavior("onkeyup") {

    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        target.add(AjaxFeedbackComponent.this.feedback);
        AjaxFeedbackComponent.this.onUpdate(target);
    }

    @Override
    protected void onError(AjaxRequestTarget target, RuntimeException e) {
        super.onError(target, e);
        target.add(AjaxFeedbackComponent.this.feedback);
    }
});

按预期效果很好.但是,出于可用性的原因,我不希望某些验证程序在Ajax表单处理中运行,而仅希望在提交最终表单时运行它们.我知道 IFormValidator ,它在 AjaxFormComponentUpdatingBehavior 事件处理期间未调用,但是似乎在向表单中添加验证器(实际上是相关的验证器)时感觉到了代码的味道仅单个字段,并且应封装在各个组件中,例如在 UsernameRegistrationField 中.

This works great, as expected. However, for usability reasons I do not want some of the validators to be run within the Ajax form processing, but I only want to run them when the final form is submitted. I'm aware of IFormValidator, which are not called during the AjaxFormComponentUpdatingBehavior event processing, but it seems like a code smell to add validators to the form that actually are validators that concern single fields only and should be encapsulated in the respective from components, say within UsernameRegistrationField.

例如,如果我设置了 formComponent.setRequired(true),并且用户使用TAB键浏览了表单,则在用户执行操作之前会触发 onkeyup 事件甚至开始输入用户名-并立即显示验证器的 .Required 错误消息.这对于用户来说有点尴尬.因此,我希望能够将表单处理所需的字段标记为必需,但是跳过ajax处理中所需的验证.由于某些其他原因,我也想将其他验证器(有些代价高昂")延迟到最终的表单处理中.

For example, if I set formComponent.setRequired(true), and the user navigates through the form using TAB keys, the onkeyup event is fired before the user has even started typing a username -- and immediately the validator's .Required error message is shown. This is a bit awkward for the user. Therefore I'd like to be able to mark a field as required for the form processing, but skip the required validation in the ajax processing. For some other reasons I also want to delay (some "costly") other validators to the final form processing.

所以我的问题是:如何在 AjaxFormComponentUpdatingBehavior 处理过程中禁用表单组件上的验证器,但是不完全禁用单个表单组件验证?还是我真的必须作为 IFormValidator 进行所有单个字段的验证?还是适合 setDefaultFormProcessing()?

So my question is: How can I disable a validator on a form component within the AjaxFormComponentUpdatingBehavior processing, but do not disable individual form component validation completely? Or do I really have to do all the individual field's validation as a IFormValidator? Or would this be a good fit for setDefaultFormProcessing()?

推荐答案

如果您使用的是非Ajax表单提交,则验证者只能通过检查是否存在ART来进行轻度"检查:

If you're using non-Ajax form submits, your validators could do 'light' checks only, by checking the existence of an ART:

boolean light = RequestCycle.get().find(AjaxRequestHandler.class) != null;

否则,您的AjaxFormComponentUpdatingBehavior可以在requestCycle的metaData中存储一个标志,这允许您的验证器(或验证器包装器)检查轻量"验证.

Otherwise your AjaxFormComponentUpdatingBehavior could store a flag in the requestCycle's metaData, which allows your validator (or a validator wrapper) to check for a 'light' validation.

这篇关于AjaxFormComponentUpdatingBehavior和表单提交的不同验证器集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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