开发者

Hibernate validator NotEmpty trim issue

开发者 https://www.devze.com 2022-12-16 07:40 出处:网络
It appears that the Hibernate NotEmpty annotation does not result in an error for strings filled with whitespace (\"\").Only works for nulls or empty strings (ie: new String()).Is there a workaround/f

It appears that the Hibernate NotEmpty annotation does not result in an error for strings filled with whitespace (" "). Only works for nulls or empty strings (ie: new String()). Is there a workaround/fi开发者_开发问答x for this?


@NotEmpty is used to check size rather than contents and applies to Collections as well as Strings. The functionality you're looking for is provided in @NotBlank which is specific to Strings and ignores trailing whitespace.


@NotBlank is the way to test string lengths with an implicit trim call.


Replace your @NotEmpty with a @Pattern annotation that includes a regex expression that will fail on strings that are pure whitespace or empty (you may be able to include both @NotEmpty and @Pattern and simplify the regex that way). Or write a custom validator as described here.

0

精彩评论

暂无评论...
验证码 换一张
取 消