Hibernate validator NotEmpty trim problem

It seems that the Hibernate NotEmpty annotation NotEmpty not produce an error for lines filled with spaces ( " " ). Only works for zeros or empty strings (i.e.: new String() ). Is there a workaround / fix for this?

+4
source share
3 answers

Replace @NotEmpty with the @Pattern annotation, which includes a regex expression that will tolerate strings that are pure spaces or empty (you can include both @NotEmpty and @Pattern and thus simplify the regular expression). Or write a special validator as described here .

+2
source

@NotEmpty is used to check size, not content, and applies to collections as well as strings. The functionality you are looking for is provided in @NotBlank, which is string specific and ignores trailing spaces.

+11
source

@NotBlank is a way to check the length of lines with an implicit trim call.

+7
source

All Articles