If player
string is null then the Size
constraint will not throw any exception. It's a best practice not to throw an error when the value is null (except for NotNull
constraint or NotEmpty
constraint). The fact is the player bean property value is probably an empty string and not a null value, so the constraint throw an exception, because JSF sends an empty string when nothing is typed in the component. In order to JSF sending a null value you have to add some content in web.xml
file as said in the Java EE Tutorial:
In order for the Bean Validation model to work as intended, you must set the context parameter javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to true in the web deployment descriptor file, web.xml:
<context-param>
<param-name>
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
</param-name>
<param-value>true</param-value>
</context-param>
If eventually (depending on versions) the parameter doesn't work as expected see this question for troubleshooting.
EDIT:
You could also try not applying the previous parameter and instead using the Pattern
constraint with a Regex pattern similar to (^$|^[a-zA-Z]{3,7}$)
.