Our goal is to validate a value stored in a field. The code I posted previously is all pseudo code.
To give you a more specific use-case: imagine that we have two forms, one for creating trouble tickets, and another for creating events. Both of these forms have a "comment" field which cannot be longer than 4000 characters. The forms are submitted via JMS to some back-end service that persists the data. I want to be able to constrain both of these GUI fields. Let's suppose that in each case I'm using a JTextArea to edit the comments. Whenever the user makes a change to the text area, I want the field to be validated using an event listener (such as an InputMethodListener).
Code:
myTextArea.addInputMethodListener(new InputMethodListener(){
public void inputMethodTextChanged(InputMethodEvent event) {
String text = myTextArea.getText();
Validator validator = ValidatorFactory.getValidator();
validator.validate(text,"comment"); //validate the text using the criteria found in the constraint set called "comment" (see below)
}
});
In this case, I cannot tie my validation criteria to the JTextArea class itself, but I must validate an instance of that class. So rather than using
Code:
<bean class="javax.swing.JTextArea">
<constraint ...
</bean>
I want to be able to use:
Code:
<constraint-set id="comment">
.... some constraints ....
</constraint-set>
And simply refer to these constraint sets from anywhere in my code.
In answer to your earlier question about backing data classes - most Swing applications don't use them. And we don't want to do a round-trip to the server to validate at persistence time, we want to give the user immediate feedback about their entry.
Any suggestions would be appreciated.
Regards,
Mark