I have a TextField where I have added an AjaxFormComponentUpdatingBehavior to get the current value when user write some string.
filterByObject = new TextField<String>("filterByObject", true, new PropertyModel<String>(searchParams, "objectFilter"));
AjaxFormComponentUpdatingBehavior changeFilterBinded = new AjaxFormComponentUpdatingBehavior ("onkeyup") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
target.addComponent(componentToUpdate);
}
};
filterByObject.add(changeFilterBinded);
When I put some chars inside textfield, onUpdate method is correctly called and my component, based on the current state of searchParams, changes correctly.
Unfortunally when I use Backspace to cancel what I have inserted, the onUpdate is not called.
I tried changing event (onkeypress, onkeydown, onchange etc…) but it doesn’t work. Only onChange works but I have to change focus to another component.
How can I save the day?
Is the input in the field invalid (according to
setRequiredorIValidators added to the field) as a result of pressing the backspace key? If it is, theonErrormethod will be called instead ofonUpdate, because user input will be invalid and therefore will not reach the ModelObject of the component with the AjaxFormComponentUpdatingBehavior.Remember that any
IFormValidatorinvolving the ajax-ified component will not execute automatically, so you might be interested in checking the input for yourself manually before updating model objects if it’s the case. You can tellAjaxFormComponentBehaviornot to update model objects automatically by overridinggetUpdateModel(). Then, in theonUpdatemethod, get the component’s new input by means ofgetConvertedInput().As a side note,
onkeyupshould be getting fired when pressing the backspace key. At least it does in this fiddle, andonchangeis generally triggered on an<input type="text">when focusing out of it.Also, HTML5 introduces the
oninputevent handler, which may better suit your needs. It will get fired even when copying/pasting in the text field. See the following link for more information: Using the oninput event handler with onkeyup/onkeydown as its fallback.