-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Validating GUI Objects
PostPosted: Wed May 11, 2011 2:54 pm 
Newbie

Joined: Thu May 05, 2011 6:15 pm
Posts: 3
I've been looking at using the JSR303 validation framework, and after studying the API and playing with the examples, I'm having some problems understanding how this would be used with GUI components. Unlike bean-oriented validation, where we're trying to validate the entries into a model, or a POJO, validating entries in a GUI may not be tied directly to a widget class but rather to a specific instance of that class.

For example, recently I needed to validate entries into a JTextArea used to edit "event.comments". These entries are used to populate a database field specified in a tables.hbm.xml file. What I'd like to be able to do is this:

Code:
// validate at the GUI level
JTextAreaValidator validator = ValidatorFactory.getValidatorForClass(JTextArea.class);
BeanDescriptor beanDesc = validator.getConstraintsForClass(JTextArea.class);

// similar to a bean descriptor, this contains a collection of ConstraintValidators associated with an ID.
SemanticDescriptor semDesc= validator.getConstraintsForInstance("event.comment"); // use the name or id of an object to get the constraints for the object.

...
validator.validate(myTextArea); // apply a consolidated list of constraints to this instance

...
// use the same semantic descriptor to validate at the database level, just in case someone forgot to validate at persistence time.
Validator beanVal = ValidatorFactory.getValidator();
SemanticDescriptor semDesc = beanVal.getConstraintsForInstance("event.comment"); // apply the same criteria to the database field.


Is there any way of doing something like this with the current API? If not, how can I do this in such a way that I can use both class-level constraints and instance constraints in a way that doesn't break the framework?

Any advice would be appreciated.

Regards,

Mark


Top
 Profile  
 
 Post subject: Re: Validating GUI Objects
PostPosted: Thu May 12, 2011 9:17 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I am completely confused on what you want to achieve. Also, what is this code about? Is this some existing home grown stuff, or is this some sort of pseudo code? And what do you mean w/

Quote:
validating entries in a GUI may not be tied directly to a widget class but rather to a specific instance of that class.


JSR303 is all about Bean Validation. So you will need some sort of backing data classes (beans). This is a pre-condition for using JSR303. I am not sure why you cannot or don't want to have data beans. For me it would makes sense to separate GUI components from the data model. Maybe I just don't understand your problem.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validating GUI Objects
PostPosted: Thu May 12, 2011 12:41 pm 
Newbie

Joined: Thu May 05, 2011 6:15 pm
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: Validating GUI Objects
PostPosted: Fri May 13, 2011 3:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
You don't have to go to the server to put the data into data containers. In your event listeners just update the domain objects or whatever you want to call them and then validate this object. Somewhere you also have to define your constraint on.
As the specification says JSR 303 is about Bean Validation, so at some stage you will have to create them.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.