How can i use @Pattern constraint on non-mandatory form fields ?
@Pattern(regexp="...")
private String something;
as soon as i submit my form, i get validation error as expected but the user may leave the field empty, since this is not a mandatory field.
PS: i could write my own constraint annotation. However, i just ask an easier way combining annotations or adding annotation attributes. JSR303 implementation is hibernate-validator.
Just set it with
nullinstead of empty string. Since empty HTML input fields are by default submitted as an empty string as HTTP request parameter, you need to let your MVC framework interpret empty submitted values asnull. This is in JSF easy done by a<context-param>inweb.xml. However, since you’re using Spring MVC and I don’t do it, I searched a bit here and found this answer which may be of use.