If I create a
StringLengthValidator with a minimum length of 3, the behavior I see when the user does data entry of a color word:
Red =
valid
Re =
invalid
R =
invalid
=
valid (empty string, length is zero)
Is that last one a feature or a bug? Seems like a bug to me: ( 0 < 3 ) ∴ invalid
Here is a tiny little example app to demonstrate the issue.
package com.example.completedlayout;
import com.vaadin.Application;
import com.vaadin.data.Validator;
import com.vaadin.ui.*;
public class CompletedlayoutApplication extends Application {
TextField favoriteColorField = null;
TextField someOtherField = null;
@Override
public void init() {
Window mainWindow = new Window( "Completedlayout Application" );
mainWindow.setImmediate( true );
VerticalLayout layout = new VerticalLayout();
// Favorite color field
this.favoriteColorField = new TextField( "Favorite color:" );
this.favoriteColorField.setImmediate( true );
// StringLengthValidator(String errorMessage, int minLength, int maxLength, boolean allowNull)
[color=#02b90e]
Validator stringLengthValidator = new com.vaadin.data.validator.StringLengthValidator( "Required field. Type 3-10 characters.", [color=#db0312]
3
[/color], [color=#db0312]
10
[/color], false );
[/color]
this.favoriteColorField.addValidator( stringLengthValidator );
layout.addComponent( this.favoriteColorField );
// Some other field - irrelevant.
this.someOtherField = new TextField( "Some other field:" );
this.someOtherField.setImmediate( true );
layout.addComponent( this.someOtherField );
mainWindow.setContent( layout );
setMainWindow( mainWindow );
}
}
StringLengthValidator does have a setting about whether to consider Null values as valid or not. But my case above is not about Null. It is about an empty String where length is zero.
I looked at the source code for StringLengthValidator, to see if I could find a bug. This key piece from the “isValid” method seems correct. So it seems some other code in some other place is overriding the results of this validity test.
if ([color=#01b825]
(minLength >= 0 && len < minLength)
[/color]
|| (maxLength >= 0 && len > maxLength)) {
return false;
}
I have a workaround. I wrote a subclass of TextField to overload AbstractTextField’s overload of the “isEmpty” method. I restore the definition of “isEmpty” to that used by AbstractTextField’s superclasses