spring - Is it possible to drive the @Size "max" value from a properties file? -
i'm using spring 3.1.1.release. have model following attribute
import javax.validation.constraints.size; @size(max=15) private string name;
i validate model in controller running
@requestmapping(value = "/save", method = requestmethod.post) public modelandview save(final model model, @valid final myform myform,
i have "15" value come properties file instead of hard-coded, unclear if that's possible or how done. ideas?
this not possible. constant expression provide value max
attribute added @ compile time. there no way change value of annotation @ runtime. setting properties file read therefore not possible
what can instead create , register own validator
class has field. example,
public class myvalidator implements validator { public void validate(object target, errors errors) { myobject obj = (myobject) target; int length = getproperties().get("max.size"); if (obj.name.length() > length) { errors.rejectvalue("name", "string length bigger " + length); } } public boolean supports(class<?> clazz) { return clazz == myobject.class; } }
take @ spring's validation framework.
Comments
Post a Comment