TextChangeListener & Validation (isValid())

In my application I use a lot of components which I’d like to validate upon change. Detecting change can be done with the TextChangeListener, but I have troubles using the validators for those fields. An example:


	TextChangeListener textChangeListener = new TextChangeListener() {
		@Override
		public void textChange(TextChangeEvent event) {
			AbstractTextField source = (AbstractTextField) event.getSource();
			System.err.println(event.getText() + " - " + source.getValue());
			doValidation();
		}
	};

	TextField textField = new TextField();
	textField.setRequired(true);
	textField.setImmediate(true);
	textField.setTextChangeEventMode(TextChangeEventMode.LAZY);
	textField.addListener(textChangeListener);

Problem is that the value of the TextField is not updated in the textChange event and hence my validation framework (using Vaadin’s built-in isValid() methods on input fields) can only validate the old data. I tried setting the value of the TextField in the textChange event, but then I get into an endless loop (see also http://dev.vaadin.com/ticket/6376 )).

How can this be solved?

Hi,

We kept the recently introduced textchangevent separate from the value change event as there wouldn’t have been an easy way to distinguish typing and actual value submit (enter/blur).

The endless loop was clearly a bug and is now resolved in the nightly builds of the 6.5 branch. Now it should be possible to do setValue(event.getText()) in text change listener.

However, due to the fact how validation is implemented in Vaadin there might be some synchronization issues in the approach. The TextField is re-rendered on the server side value change and as we don’t block the UI during the server visit, some letters might get dropped. I guess with the LAZY mode and a fast server (low latency) it may work quite fine. If there are some problems I suggest wrapping the TextField into a CustomField (and doing the validation on CustomField level + avoid textfield value changes during text change events). Haven’t tried it, but I’d expect it to work.

http://vaadin.com/directory#addon/customfield

cheers,
matti

Hi Matti,

The latest nightly seems to work. So far I didn’t bump into pieces of text being overwritten by the re-rendering, I’ll do some more tests later (and eventually refactor to CustomField).

Thanks,
Johannes

Hi guys,
adding text change listener on text field make my page scroll to top of page, do u have any idea why is happening? thnx.