Using Play 2.3 (Java), I'd like to use a parameter from application.conf as a value for a constraint. Something like:
public class Some {
@Constraints.MaxLength(Play.application().configuration().getInt("some.text.maxlength", 1000))
public String text;
}
But of course I can't do this because the annotation parameter must be a constant. What would be the approach, in Java, to bypass this? Should I use a custom validator? Or is there another option?
Any help appreciated.
As you discovered you can't use it via annotations, but fortunately you can write own constraints and read the
application.conf
within it, fast sample based onMinLength
constraint:file: app/utils/MyConstraints.java
usage:
As you can see to add more constraints like that you only need to add the pairs of
MyFooConstraint
andMyFooConstraintValidator
intoMyConstraints
class, so you can also make it more generic:and then use it like: