[font=tahoma]
The commit doesn´t throw any exception.
Even if a initialized the property name = “user” the textfield remains blank. It is like BeanFieldGropu doesn’t bind any bean with any textfield.
The difference is that in the book, the layout is created and bind the member fields with buildAndBind
and i bind with bindMemberFields using an instance of formLayout.
I tried to validate the empty textField name with three differents manners:
adding setRequired to the textField name.
adding tha annotation @NotNull in the property name of the bean.
addind a validator NotNullValidation in the textField.
The two first work but the last doesn’t.
I use the method commit of the BeanFieldGroup.
I was wonder if commit’s method validate the fields (i think so) but in my case it doesn’t throw CommitException with the validator.
This is my formLayout class.
public class MainLayout extends FormLayout{
private TextField name;
private Button valida;
public MainLayout(){
name = new TextField("Nombre: ");
name.setValidationVisible(false);
name.setNullRepresentation("");
name.addValidator(new NullValidator("Debes introducir un nombre", true));
addComponents(name, valida);
}
...
The listener of the button valida is in a View
...
layout.getValida().addClickListener(e-> {
try{
layout.visible(); //set the validation visible to true
binder.commit(); //binder is the instance of BeanFieldGroup
} catch(CommitException ce){
Notification.show(ce.getMessage(), Notification.Type.WARNING_MESSAGE);
}
});
Why commit doesn’t throw the Exception when the TextField name has a NullValidator?
I Found in the API the response.
i was misunderstood the constructor of NullValidator.
NullValidatorpublic NullValidator(java.lang.String errorMessage, boolean onlyNullAllowed)Creates a new NullValidator.
Parameters:
errorMessage - the error message to display on invalidation.
onlyNullAllowed - Are only nulls allowed?
It must be
name.addValidator(new NullValidator("Debes introducir un nombre", false));
It’s a bit confused NullValidator to validate not null fields!!! if you put the second param true.