Hello,
I'm using a String to build entities. How can I validate this String ?
Here is an example :
Code:
@Entity
class Question {
[...]
@OneToMany( cascade=CascadeType.ALL)
private List< Option > options;
transient String optionsText;
public void setOptions(List<Option> options) {
this.options = options;
// convert options in optionsText
[...]
}
public void setOptionsText(String optionsText) {
// convert optionsText in options
String lines[] = optionsText.split( separator );
[...]
}
[...]
}
Code:
class Option {
[...]
@Length(min=1, max=50)
private String text;
}
So I would like to validate all the produced options. I quickly imagined something like :
Code:
class Question {
[...]
@TransformationConstraint( "optionMaker" )
transient String optionsText;
@Transformator( "optionMaker" )
public static List< Option > makeOptions( String optionsText ) {
[...]
}
public void setOptionsText( String txt ) {
// use makeOptions
[...]
}
}
The @Transformator annotation register a transformator with id optionMaker. Then we can use it in @TransformationConstraint.
Ok this is just an idea, maybe something easier / cleaner is possible.
Thanks for your comments.
Bruno