Hi,
I just read the article and tested it. I put a validation.xml with the following content in the META-INF folder:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<constraint-mappings
xmlns="http://jboss.org/xml/ns/javax/validation/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd">
<constraint-definition annotation="javax.validation.constraint.Size">
<validated-by include-existing-validators="true">
<value>de.kba.zfer.core.util.beanvalidation.validator.SizeValidatorForStringDelegate</value>
</validated-by>
</constraint-definition>
</constraint-mappings>
I read in the logging, that hibernate validator founds and parses the file:
Quote:
2011-06-22 08:14:01,254 | INFO | ValidationXmlParser | - META-INF/validation.xml found. Parsing XML based configuration.
But I get an error during executing the validation, that there is no validator found for my custom type:
Quote:
javax.validation.UnexpectedTypeException: No validator could be found for type: de.kba.zfer.core.model.common.datatypes.FahrerlaubnisNummer
This is my validator:
Code:
package de.kba.zfer.core.util.beanvalidation.validator;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraints.Size;
import de.kba.zfer.core.model.common.datatypes.FahrerlaubnisNummer;
public class SizeValidatorForStringDelegate implements ConstraintValidator<Size, FahrerlaubnisNummer> {
private int min;
private int max;
@Override
public void initialize(Size constraintAnnotation) {
this.min = constraintAnnotation.min();
this.max = constraintAnnotation.max();
validateParameters();
}
@Override
public boolean isValid(FahrerlaubnisNummer value, ConstraintValidatorContext context) {
// sollte das gegebene StringDelegate-Object null sein, gib true zurueck
if (value == null) {
return true;
}
// ansonsten pruefe, ob die Laenge zwischen min und max liegt
return value.length() >= min && value.length() <= max;
}
private void validateParameters() {
if (min < 0) {
throw new IllegalArgumentException("The min parameter cannot be negative.");
}
if (max < 0) {
throw new IllegalArgumentException("The max parameter cannot be negative.");
}
if (max < min) {
throw new IllegalArgumentException("The length cannot be negative.");
}
}
}
Can somebody tell me my mistake?
Thanks and kind regards,
Andreas