Search code examples
validationvaadincustom-validators

[vaadin]add a validator in a field which depends another field in a form with "form.setBuffered(true);"


I had a question.that is if I use "form.setBuffered(true);",I can't validate like this:

field.addValidator(new Validator() {
    @Override
    public void validate(Object o) throws InvalidValueException {
        //FIXME There is the problem.if i set "form.setBuffered(true);",I can't get latest testBean.
        if (testBean.getId().equals(testBean.getName())) {
            throw new Validator.InvalidValueException("id can't equals name.");
        }
    }
});

There is the code: github

so,an way could fix it?

sorry for my poor english.


Solution

  • The idea of buffered is to keep the bean intact and only write it with commit. So your tests there read the current value of your bean (which holds the initial values, which are OK, hence no validation errors). If you commit once with wrong data, you will see, that you never again can commit, because now the validation triggers.

    One way around this is too remember the fields for this case and compare the current values of the field for name and id. Before commit, they are the only source for user input.