I have a wicket form which contains many TextField input components. There’s a Validator attached to most of these inputs.
Suppose I’ve entered 50 values, and one of those fails a range validator. Wicket then generates an error feedback message, but also does not update the models associated with each component. Result is that I lose all 50 values I just entered, and have to type them in again.
My question is, can I tell Wicket to update the models of those components which had valid values, but just report an error for the bad value?
Digging around in the framwork, I noticed this code fragment in FormComponent, which seems to be indicating that if there is an error, then don’t update the model.
public final void processInput()
{
inputChanged();
validate();
if (hasErrorMessage())
{
invalid();
}
else
{
valid();
updateModel();
}
}
Is there any way to customise this behaviour, and achieve my aim of retaining all valid values?
I’d bet that
FormComponent.processInput()is not being called at all here. When you submit theForm,Form.process()gets called. There, it will callForm.validate(), which in turn will callForm.validateComponents(), ultimately usingFormComponent.validate().The problem you’re having here is the global handling in
Form.process(). The form submits fully, or not at all. When aFormComponent.validate()fails,Form.hasError()will returntrue, and thereforeForm.process()will never update any of the models.You could:
FormValidator. There you can choose to update the Models of thoseFormComponentsthat pass their validations.Form.onError()and use a visitor there to update the models of validFormComponents.Form.process()and modify the// If a validation error occurredbranch to use your own methods to mark components as valid/invalid, and update (or not) model objects, depending on theFormComponenthaving errors.FormComponent.hasErrorMessage()will tell you if validation failed on a certainFormComponent.UPDATE
After discussing the reasons why user input was lost with the OP, it turned out that the
FormComponentswere being added in aListViewthat did not havesetReuseItemsset totrue. This was causingFormComponentsto be created anew on eachListView.populateItem(), therefore losing all user input.More information on the nature of this problem can be found here: