Is it possible to return a feedback error, but still allow the gates to update component models?

I have a gate that contains many TextField input components. Most of these inputs have a validator.

Suppose I entered 50 values ​​and one of them did not pass the range validator check. Wicket then generates an error message, but also does not update the models associated with each component. The result is that I lose all 50 values ​​that I just entered and need to be entered again.

My question is: can I tell Wicket about updating models of those components that have valid values, but just report an error for a bad value?

Delving into the framework, I noticed this piece of code in the FormComponent, which seems to indicate that if there is an error, then do not update the model.

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

Is there a way to customize this behavior and achieve my goal of preserving all valid values?

+6
source share
1 answer

I bet that FormComponent.processInput() is not called at all. When you submit Form , Form.process() called. There he will call Form.validate() , which in turn will call Form.validateComponents() , ultimately using FormComponent.validate() .

The problem you are facing is the global processing in Form.process() . The form fully submits an application or not at all. When a FormComponent.validate() fails, Form.hasError() will return true , and so Form.process() will never update any of the models.

You can:

  • Move all independent FormComponent checks to FormValidator . There you can update the Models of those FormComponents that pass their checks.
  • Form.onError() and use the visitor there to update the models of the valid FormComponents .
  • Override Form.process() and change the // If a validation error occurred branch to use your own methods for marking components as valid / invalid and updating (or not) model objects, depending on FormComponent errors. FormComponent.hasErrorMessage() will FormComponent.hasErrorMessage() you if validation fails on a specific FormComponent .

UPDATE

After discussing the reasons why user input was lost using the OP, it turned out that FormComponents were added to the ListView , which did not have setReuseItems set to true . This caused the creation of FormComponents anew on each ListView.populateItem() , so any user input is lost.

More information on the nature of this problem can be found here :

However, there are several points that you must observe when using a repeater in the form. Repeaters usually clean their items at the beginning of each request, when inside the form this is usually undesirable because you want to keep old items, because you want them to keep their state, and not recreate fresh ones.

For example, if you use ListView, you should call ListView.setReuseItems (true) inside the form to save the old ones instead of creating new ones each time.

+7
source

Source: https://habr.com/ru/post/900606/


All Articles