StringLengthValidator doesn't recognize null value

Hello everyone,

I’m working on a part of a web application currently where the user is supposed to fill a form. Inside of this form there is a
on which I’ve added a StringLengthValidator. The StringLengthValidator should mark the TextField as invalid if the String inside the TextField is < 5 or > 20 or null. So far so good, it worked as expected. The thing that annoyed me was, that everytime you open the view to fill in the form the TextField “userTypeField” would be shown in red by default, because it’s null obviously. I decided to set a “setNullRepresentation” so it wouldn’t be shown as red right from the beginning. Furthermore I added a “FocusListener” which sets the NullRepresentation to null once you enter the field. So basically, once you leave the field without typing anything the value is null and the validator should recognize that and mark the userTypeField red because it is invalid. Unfortunately it doesn’t do that. I’ve also tested if the value inside the userTypeField is really null with an “isEmpty()” and it’s been true.

Am I missing something here or could it be a bug inside the Validator?
I’d be very grateful for any help as I’m really stuck here and I just can’t spot the fault.

userTypeField = new TextField();
        "Geben Sie dem Kontingent einen Namen der zwischen 5 und 20 Zeichen lang ist.");
    //TODO: Der Validator erkennt anscheinend keine null value
    userTypeField.addValidator(new StringLengthValidator(
        "Die Profil-Id ist ungültig. " + "Die Id muss zwischen 5 und 20 Zeichen lang sein "
            + "und darf keine Leerzeichen enthalten.",
        5, 20, false));

    userTypeField.addFocusListener(new FocusListener() {

      public void focus(FocusEvent event) {

The “StringLengthValidator” does indeed treat null as a valid value, but in that case, the option “setRequired” should catch the missing value. You can also add a “NullValidator” to make sure the value is validated against null.

Hey Zixxl,

thank you for your reply. I found out that the problem is the combination of “setRequired” and “StringLengthValidator”, once I disable “setRequired” the “StringLengthValidator” works as expected. If both are active it seems to give a conflict between the “non-empty” validator which “setRequierd” sets and the “StringLengthValidator”.

I’ve also tried to use a “NullValidator” but it didn’t seem to work either, well, I’ll give it another try.