Validation using TextChangeEvent: how to validate most recent data?

(note: see also
http://vaadin.com/forum/-/message_boards/view_message/564617
)

I am trying to implement a live validation framework (similar as chan guang). The TextChange event is the only one I can use, since that event gives me updates during typing. Unfortunately the actual value is still the old one as you describe, which results in a isValid() call which uses the old value and hence is not usable.

I’ve tried setting the value of the component using setValue(event.getText()) but that introduces unexpected behavior (text parts missing, cursor position changes in internet exporer, …). The ValueChangeListener would be an excellent event for me to use - if it would be triggered during text change and not only after unfocus.

Live validation as described on http://livevalidation.com/examples is what I’d like to achieve - but I couldn’t find a sample implementation using Vaadin. Do you have any pointer on how to solve this?

Thanks,
Johannes

I’ve implemented the suggestion by Marc Englund (see other thread in first post), but unfortunately without success.

When I test the application I now get the behavior that my newly entered data is overwritten by an empty (the previous?) value. Thus, whatever data I try to enter is removed again. Any more clues how to solve this?

Being able to validate text while entering seems like standard functionality to me - I hope we’ll find a solution to this problem soon.

Thanks,
Johannes

The problem appears to be coming from the implementation I did. At line 38 I’m adding a style to my TextArea which results in a repaint. This repaint causes the TextArea to loose the newly entered value and hence makes my validation framework to be not working.

Is there a way to make either set css styles another way (I’m using CustomLayouts) or to make a distinction between setting/adding styles and changing field values?

Johannes



	protected void refresh(String parameters) {
		CustomLayout mainLayout = new CustomLayout("test");
		TextField textField = new TextField();
		textField.setTextChangeEventMode(TextChangeEventMode.LAZY);
		textField.addListener(textChangeListener);
		mainLayout.addComponent(textField, "testField");
		setContent(mainLayout);
	}

	TextChangeListener textChangeListener = new TextChangeListener() {
		@Override
		public void textChange(TextChangeEvent event) {
			AbstractTextField source = (AbstractTextField) event.getSource();
			isValid(source, event.getText());
		}
	};


	public boolean isValid(AbstractField field, String newValue, boolean initialRun) {
		if (field.isRequired()) {
			if (StringUtils.isEmpty(newValue)) {
				valid = false;
			} else {
				valid = true;
			}
		}

		for (Validator v : emptyIfNull(field.getValidators())) {
			try {
				v.validate(newValue);
			} catch (InvalidValueException e) {
				valid = false;
			}
		}
		if (!valid) {
			field.addStyleName("error");
		}
	}