Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT Validator with Editor Framework

Have anybody realized how editors and jsr 303 validation work with GWT 2.3 coming? Validation API was add to gwt sdk. But I am unable to validate entities using the editor framework. No matter what I do the errors are never thrown from client side or server side.

Here is a code snippet:

public class P {

  public P() {}

  @Size(min=4)
  private String name;

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

}

PEditor

public class PEditor extends Composite implements Editor<P> {

  private static PEditorUiBinder uiBinder = GWT.create(PEditorUiBinder.class);

  interface PEditorUiBinder extends UiBinder<Widget, PEditor> {}

  @UiField
  TextBox name;

  public PEditor() {
    initWidget(uiBinder.createAndBindUi(this));
  }

}

  PEditor pEditor;
  interface Driver extends SimpleBeanEditorDriver<P, PEditor> {}

  Driver driver = GWT.<Driver> create(Driver.class);

  public void onModuleLoad() {

    pEditor = new PEditor();
    driver.initialize(pEditor);
    P p = new P();
    driver.edit(p);
    pEditor.name.setText("G");
    driver.flush();

    if(driver.hasErrors()) {
        List<EditorError> errors = driver.getErrors();

        for (EditorError error : errors) {
          System.out.println(error.getMessage());

        }

    }
  }

Thanks for your help

like image 366
user709433 Avatar asked May 12 '11 13:05

user709433


1 Answers

The Validation API, at least as of 2.3, doesn't build client side code for you – it is a tool that can be integrated on the server to make your server spit back errors in certain cases.

The call to EditorDriver.hasErrors() is just to check if any code has told the local delegates if there are errors – client-side validation can be implemented through this.

The most automatic case right now is when using RequestFactory - if you have the javax.validation jar (both api and sources) on your server classpath as well as a validation library (hibernate-validator and apache's bval are two such libraries), the Receiver callback will have onViolation called.

With RequestFactory being used to get violations from the server, the RequestFactoryEditorDriver can then be used to push errors to the UI, though the use of HasEditorErrors editor instances and wrappers, like ValueBoxEditorDecorator, or just by notifying the user through some mechanism (alert, banner, your debug sys.out.println, etc) when onViolation is called.

If using RPC, you can run the server validations on your own, and (as of 2.3) call driver.setConstraintViolations with the ConstraintViolation objects generated on the server from the validation process.

like image 167
Colin Alworth Avatar answered Nov 15 '22 10:11

Colin Alworth