hibernate validator LengthValidator of truncated string

て烟熏妆下的殇ゞ 提交于 2019-12-11 14:03:39

问题


Is there a similar annotation to @Length in hibernate validator but it restricted on a trimmed string.

For example:

I have

@Length(max= 2);
String str;

when: str = " ab";
--> it will pass ok.

Thanks,


回答1:


As fare i know, there isn't one.

But you can easely implement your own.

The other (not so good) way is to add a getter which trim the string.

@Length(max= 2);
String getTrimmedStr() {
   return this.str.trim();
}

But i think implementing your own Validator is the much cleaner approach.




回答2:


I don't think so, but you can easily implement one with javax.validation, by extending the LengthValidator



来源:https://stackoverflow.com/questions/4432719/hibernate-validator-lengthvalidator-of-truncated-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!