2

I would like to use the ValidationSupport to check the content of textfield2 against the content of textfield1. Here is what I tried to do :

validationSupport = new ValidationSupport();

validationSupport.registerValidator(textfield2, false, (Control c, String newValue) 
    -> ValidationResult
         .fromMessageIf(textfield2, 
                        "Should contain texfield", 
                        Severity.WARNING, 
                        !newValue.contains(textfield1.getText())));

validationSupport.initInitialDecoration();

Filling textfield1 first, then texfield2 is working as I expect : the warning decoration is displayed on textfield2 until the textfield1 content is not included in the texfield2 content.

But starting from same content in textfield 1 & 2, and trying to change texfield1 again (hence at this point, textfield1 content differ from textfield2 content), I was expecting the warning decoration to be displayed immediately on texfield2, which is not the case !

Indeed in order to have the validationSupport checked again, I need to update the textfield2 again. Only after the texfield2 content has been updated, then the ValidatorSupport is checked again, and the decoration is updated accordingly !

The only way I found to always get the decoration up to date is to remove the previous registerValidator and to add a listener on textfield1 textProperty to register a new validator for texfield2 each time textfield1 content is changed :

textfield.textProperty().addListener((observable, oldValue, newValue)
  -> validationSupport.registerValidator(textfield2, false, (Control c, String str)
    -> ValidationResult
         .fromMessageIf(textfield2,
                        "Should contain texfield1",
                        Severity.WARNING,
                        !str.contains(textfield.getText()))));

This is working (meaning when I change textfield1, decoration on textfield 2 is updated immediately) but I feel it's not the right solution, but can't find another / better one. Is it the right way to do ? If not, what is the right way ? Any recommendation ? Thanks.

Thierry
  • 21
  • 6

0 Answers0