Hibernate character validator

Is it possible to verify that the character is either M or F, or do I need to use a string with a regular expression?

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

I would like to use

 private Character sex; 
+4
source share
1 answer

It must be a regular expression to accept only M or F.

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

In your second use case as a character, you need to confirm that the character is "M" or "F". Another can be set as sex.

You cannot use @Pattern for the Character variable, you will get below exceptions.

javax.validation.UnexpectedTypeException: HV000030: the validator cannot be found for type: java.lang.Character.

+6
source

Source: https://habr.com/ru/post/1436336/


All Articles