javagwtrequestfactorygwt-editorsgwt-validation

Gwt Client Validation with editor and request factory


I had a problems with GWT Client Validation used with RequestFactory and Editors.

The edit code is :

LocalityRequest localityContext = //create Request 
Locality locality = //Locality Entity Proxy loaded from the server
driver.edit(locality, localityContext); //Edit the proxy
request = localityContext.updateLocality(locality);

And the save code is :

this.localityContext = (LocalityRequest) driver.flush(); //Flush the request
Set<ConstraintViolation<LocalityProxy>> violations = validator.validate(this.locality); //Local validate the object
if (!violations.isEmpty()) {
    Set<ConstraintViolation<?>> sets = new HashSet<ConstraintViolation<?>>(violations);
    driver.setConstraintViolations(sets);
    editLocalityView.setErrors(sets); //give errors to the editors
    return;
}
localityContext.fire(); //else send the request

My problems is that the local validation always validate on the loaded version and not the edited by user version. How can we get the flushed object saved in the request ?

thanks


Solution

  • You need to cache/store the edited object (see here for more details):

    LocalityRequest localityContext = //create Request 
    Locality locality = //Immutable Locality Entity Proxy loaded from the server
    Locality modifedLocality = ctx.edit(locality); // Create Mutable copy 
    driver.edit(modifedLocality, localityContext); //Edit the mutable proxy
    request = localityContext.updateLocality(modifedLocality);
    

    And in the save code:

    this.localityContext = (LocalityRequest) driver.flush(); //Flush the request
    Set<ConstraintViolation<LocalityProxy>> violations = validator.validate(this.modifedLocality); //Local validate the mutable Proxy
    if (!violations.isEmpty()) {
        Set<ConstraintViolation<?>> sets = new HashSet<ConstraintViolation<?>>(violations);
        driver.setConstraintViolations(sets);
        editLocalityView.setErrors(sets); //give errors to the editors
        return;
    }
    localityContext.fire(); //else send the request