I'm new to Spring, and I'm curious as to how to approach validating form input against a data source. I'm using Spring 3.0.3, by the way. So lets say I have the following form:
public class MyForm {
private String format;
public String getFormat() {
return format;
}
public void setFormat( value:String ) {
format = value;
}
}
Now lets say the format
property is a string representation of a file format: "jpg", "png", "gif", "bmp", etc. Naturally I thought to write a validator for the form. It looks something like this:
public class MyFormvalidator implements Validator
{
@Override
public boolean supports( Class<?> clazz ) {
return MyForm.class.equals( clazz );
}
@Override
public void validate( Object obj, Errors errors ) {
MyForm myForm = (MyForm) obj;
ValidationUtils.rejectIfEmptyOrWhitespace( errors, "format", "error.required" );
if( !myForm.getFormat().equals( "jpg" ) &&
!myForm.getFormat().equals( "png" ) &&
.... etc .... ) {
errors.rejectValue( "format", "error.invalid" );
}
}
}
So thats all fine and dandy, but lets say I want to add new supported formats without having to adjust my validator, recompile, and deploy it. Rather than get into database 'n what not, lets keep it simple go with a little XML file that could be modified. It would look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<fileformats>
<item>jpg</item>
<item>png</item>
.... etc ....
</fileformats>
And finally, the real point of all this...what should my approach be to making this data available during the validation of the form input?