Hibernate validator for chars

别说谁变了你拦得住时间么 提交于 2019-12-01 17:35:30

问题


Is it possible to validate that a Character is either M or F or do I need to use a string with a regex?

@Pattern(regexp = "^[MF]{1}$", message = "customer.sex.regex")
private String sex;

I would like to use

private Character sex;

回答1:


Your should this regular expression for accepting only M or F.

@Pattern(regexp = "^[M|F]{1}$", message ="Must be M or F")

In your second case of using as Character, you need to validate that this character is whether "M" or "F". Other can be set as sex.

You cannot use @Pattern for Character variable, You will get below exception.

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Character.



来源:https://stackoverflow.com/questions/12599069/hibernate-validator-for-chars

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