Validator is called more than once.

I have a validator attached to the field. When I click the button the validator will be called twice. Is that a bug? I think it makes no sense to call the validator many times. Following is the test code:

package com.sdk;

import com.vaadin.Application;
import com.vaadin.data.validator.AbstractValidator;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

/**
*
*/
public class ApplicationMain extends Application {

@Override
public void init() {
    Window mainWindow = new Window();
    final Form testForm = new Form();
    TextField field1 = new TextField("Name:");
    field1.addValidator(new AbstractValidator("sdsd") {
        public boolean isValid(Object o) {
            System.out.println("Validating field1..");
            if (o.equals("David")) {
                return true;
            }
            return false;
        }
    });
    testForm.addField("testField1", field1);

    Button button = new Button("Apply");
    button.addListener(new Button.ClickListener() {
        public void buttonClick(Button.ClickEvent clickEvent) {
            if (!testForm.isValid()) {
                return ;
            }
            System.out.println("form is valid.");
        }
    });
    testForm.getFooter().addComponent(button);
    mainWindow.addComponent(testForm);
    setMainWindow(mainWindow);
}

}

Most of the Field calls seem to occur often. It seems every repaint request ends up calling, especially when it’s initially set or when you change the value.

I noted that it was called when the Form is initially displayed with my initial values. Makes sense, so even if the initial value I set it to is not valid, it will start out marked invalid.

Then when I change a value, it is called once via a ‘handleUidlRequest’ that calls ‘paintAfterVariableChanges’. It is called with the new value, so again, this makes sense.

But it doesn’t, because it gets called again with the new value, this time as part of my buttonClick callback where I call isValid() before I decide whether to commit() or not. I need this check because I can display some extra tips/help that the validator perhaps cannot make as clear, plus making it clear that the button press didn’t succeed in the saving of the value.

And then it gets called again, this time because I call Form.commit(). Oddly, it gets called twice in the Form.commit() processing.

Then it gets called one more time via the aforementioned handleUidlRequest/paintAfterVariableChanges mechanism.

I know in general that Field creation and validators are best to be lightweight, just like if you use BeanItems, you want to make sure your getter functions are lightweight, and if heavyweight, you’ll want to cache the results in your bean because they are called frequently, many more times that you’d expect.