问题
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);
}
});
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
.
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.
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()
?
回答1:
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;
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.
来源:https://stackoverflow.com/questions/23967621/different-set-of-validators-for-ajaxformcomponentupdatingbehavior-and-form-submi