I am using xml file to specify my constraints on an incoming request bean. I do this in a validation-constraints.xml file as follows:
<?xml version="1.0" encoding="UTF-8"?>
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
<default-package>com.mathworks.internal.business.geoTypes
</default-package>
<bean class="GetStatesByCountryCodeRequest">
<field name="countryCode">
<constraint annotation="org.hibernate.validator.constraints.NotBlank" />
</field>
</bean>
</constraint-mappings>
I also need the countryCode to be only 2 characters long. I looked at the length validator provided by hibernate but it requires a min and a max value which I am guessing in my case should be 2 and 2. How can I specify the length constraint in a XML way? I cannot find any examples for that.
I ended up doing the following:
This seems to have fixed the problem for now.
To solve your issue the Pattern Constraint would be better I think.
This would define a length 2 string.
Instead of the violation message "size must be between 2 and 2"
you would have "must match "[a-zA-Z]{2}""