0

My problem is that I want use bean validation , so I do on my employee bean:

   @Pattern( regexp = "\.+",message="Name is not null") @Size(max=10,message = "Name is verylong") 
        private String name;

I want to check with pattern if the string is null. The problem is that when submit form if the field is length>10, it gives me the error. But if it is null it doens't five me the error. Anyone can help me?

Doppu
  • 433
  • 2
  • 5
  • 14

1 Answers1

1

If you want to validate that the field cannot be null, you additionally need the annotation javax.validation.constraints.NotNull:

        @Pattern( regexp = "\.+",message="Name is not null") 
        @Size(max=10,message = "Name is verylong") 
        @NotNull
        private String name;

Consider how this would work if your field was permitted to be null, but if not null it had to match a specific pattern - you would need to have some way of making a regexp that explicitly matched a null value (and Size, and other annotations would need to do the same).

Instead, nulls are not considered as needing to be checked by the built-in validation annotations - your Pattern is asking if the string is empty (i.e. "contains at least one character"), not null - you might want to just use Size's min value instead?

Colin Alworth
  • 17,801
  • 2
  • 26
  • 39