How to validate TextBox fields?

3,949 views
Skip to first unread message

membersound

unread,
Jan 7, 2013, 8:45:34 AM1/7/13
to google-we...@googlegroups.com
I'm having problems in getting started with GWT Validation. I just want to validate some form fields on a page.
I started like this, but don't know how to go on:

class Person {
  @Size(min = 4, message = "Name too short.")
  private String name;

  Person(name) {
     this.name = name;
  }
}

class MyForm {
    @UiField
    TextBox name;

    @UiHandler
    void onClick("saveButton") {
        new Person(name);
        //...
    }
}


What do I have to do in order to validate the field now, and show the error message from the annotation on the page?

Jens

unread,
Jan 7, 2013, 8:58:56 AM1/7/13
to google-we...@googlegroups.com
Take a look at: https://developers.google.com/web-toolkit/doc/latest/DevGuideValidation

The GWT SDK also contains a small validation example project that validates a Person before sending it to the server.

-- J.

membersound

unread,
Jan 7, 2013, 9:45:53 AM1/7/13
to google-we...@googlegroups.com
Yes I know this side. But it still does not tell me:
- how to validate the name before creating the person object (which I'm looking for in terms of numbers like int age; There I'd first like the validator to catch if it's a valid age before creating the person object)
- how to display an annotation error message beneath a textbox

Could you help me out?

Jens

unread,
Jan 7, 2013, 10:34:17 AM1/7/13
to google-we...@googlegroups.com

Yes I know this side. But it still does not tell me:
- how to validate the name before creating the person object (which I'm looking for in terms of numbers like int age; There I'd first like the validator to catch if it's a valid age before creating the person object)

Your validation annotations are defined on the person so you have to create a person, fill its annotated properties with values and then validate that person. How should the validation framework know what to validate if you dont provide a person instance?
 

- how to display an annotation error message beneath a textbox

Create a error message label that you can show/hide/update based on the ConstraintViolation you get from validating your class. If Person.name has a validation violation then constraintViolation.getPropertyPath() should be "name". 

In the validation example, all messages are appended into a single message and then displayed using a label (method: sendPersonToServer()):

If your app is big enough it is maybe worth it to also look into GWT's editor framework which will save you from writing a lot of "glue" code between your UI and your model classes you want to edit.

-- J.

Thomas Broyer

unread,
Jan 7, 2013, 10:42:17 AM1/7/13
to google-we...@googlegroups.com


On Monday, January 7, 2013 3:45:53 PM UTC+1, membersound wrote:
Yes I know this side. But it still does not tell me:
- how to validate the name before creating the person object (which I'm looking for in terms of numbers like int age; There I'd first like the validator to catch if it's a valid age before creating the person object)

If you want to use the annotations that are on your Person fields, then you have to create a Person object.
Technically, I believe you could generate code, the generator reading the annotations and producing code that will be able to validate data before creating the Person object, but there's no such things in GWT proper.
That being said, creating a Person object shouldn't be that expensive that you want to avoid it a all costs.

- how to display an annotation error message beneath a textbox

Well, use an Image widget? or a Label widget? call addStyleName on the textbox?
You'd still have to manually "map" the error to the appropriate widget though.
 
Could you help me out?

The easiest is probably to use the Editor framework:
  • Each Editor has the possibility of doing some validation by itself (implement HasEditorDelegate and then call reportError on the EditorDelegate; if you want to do that only on flush() rather than dynamically as the textbox value changes, then implement ValueAwareEditor)
  • errors passed to EditorDriver#setConstraintViolations are automatically mapped to the corresponding editors, which can implement HasEditorErrors to be notified and display the errors (the way they want); ValueBoxEditorDecorator is one such widget provided out-of-the-box (though not configurable so you'll generally want to build your own)
Your onClick will then:
  1. flush()
  2. check errors from editors
  3. possibly validate using javax.validation, and then call setConstraintViolations to display them (beware that it then clears the one reported by editors themselves, you have to merge both lists of errors)
Using the Editor framework though you'll be editing an existing Person object (and/or create an empty Person object specifically to edit it and save it later), which also means it has to be mutable.

Nick Chalko

unread,
Jan 7, 2013, 10:50:03 AM1/7/13
to google-we...@googlegroups.com
On Mon, Jan 7, 2013 at 7:42 AM, Thomas Broyer <t.br...@gmail.com> wrote:


On Monday, January 7, 2013 3:45:53 PM UTC+1, membersound wrote:
Yes I know this side. But it still does not tell me:
- how to validate the name before creating the person object (which I'm looking for in terms of numbers like int age; There I'd first like the validator to catch if it's a valid age before creating the person object)

If you want to use the annotations that are on your Person fields, then you have to create a Person object.
Technically, I believe you could generate code, the generator reading the annotations and producing code that will be able to validate data before creating the Person object, but there's no such things in GWT proper.
That being said, creating a Person object shouldn't be that expensive that you want to avoid it a all costs.




validateValue

<T> java.util.Set<ConstraintViolation<T>> validateValue(java.lang.Class<T> beanType,
                                                        java.lang.String propertyName,
                                                        java.lang.Object value,
                                                        java.lang.Class<?>... groups)
Validates all constraints placed on the property named propertyName of the class beanType would the property value be value

ConstraintViolation objects return null for ConstraintViolation#getRootBean() and ConstraintViolation#getLeafBean()

Parameters:
beanType - the bean type
propertyName - property to validate
value - property value to validate
groups - group or list of groups targeted for validation (default to Default)
Returns:
constraint violations or an empty Set if none
Throws:
IllegalArgumentException - if beanType is null, if propertyName null, empty or not a valid object property or if null is passed to the varargs groups
ValidationException - if a non recoverable error happens during the validation process




--
Nick Chalko | Software Engineer | nch...@google.com |   :-) 
Generate your java flags for easier testing.
go/java_flags

Thomas Broyer

unread,
Jan 7, 2013, 10:57:13 AM1/7/13
to google-we...@googlegroups.com


On Monday, January 7, 2013 4:50:03 PM UTC+1, Nick Chalko wrote:



On Mon, Jan 7, 2013 at 7:42 AM, Thomas Broyer <t.br...@gmail.com> wrote:


On Monday, January 7, 2013 3:45:53 PM UTC+1, membersound wrote:
Yes I know this side. But it still does not tell me:
- how to validate the name before creating the person object (which I'm looking for in terms of numbers like int age; There I'd first like the validator to catch if it's a valid age before creating the person object)

If you want to use the annotations that are on your Person fields, then you have to create a Person object.
Technically, I believe you could generate code, the generator reading the annotations and producing code that will be able to validate data before creating the Person object, but there's no such things in GWT proper.
That being said, creating a Person object shouldn't be that expensive that you want to avoid it a all costs.




validateValue

<T> java.util.Set<ConstraintViolation<T>> validateValue(java.lang.Class<T> beanType,
                                                        java.lang.String propertyName,
                                                        java.lang.Object value,
                                                        java.lang.Class<?>... groups)
Validates all constraints placed on the property named propertyName of the class beanType would the property value be value

ConstraintViolation objects return null for ConstraintViolation#getRootBean() and ConstraintViolation#getLeafBean()


Ooh, great! Thanks for the tip Nick.

membersound

unread,
Jan 8, 2013, 3:17:27 AM1/8/13
to google-we...@googlegroups.com
I was not aware of the editor framework. This looks very promising!
Reply all
Reply to author
Forward
0 new messages