質問

I'm havin a problem editing a proxy from a ListEditor, and flushing the modifications back to the ListEditor, if the edited proxy has Constraint Validations.

I have the following case: A user has one or many Addresses. For a simpler example, the Address object has an ID, and a String property(that represents the proper address.

The AddressProxy it's defined in the following lines:

@ProxyFor(value = Address.class, locator = AddressLocator.class)
public interface AddressProxy extends EntityProxy {
    public Long getId();
    public void setId(Long id);

    @NotNull(message = "The address must not be NULL.")
    public String getAddress();
    public void setAddress(String address);
}

So I have an UserEditor (Editor<"UserProxy>), that has a ListEditor<"AddressProxy, LeafValueEditor<"AddressProxy>>

The ListEditor is mapped to a CellTable, and I have a button after the table, to edit a certain Address from the Addresses List.

If I edit an Address from the list, and it has Constrain Validation errors(i delete the address String), the address from the ListEditor it's updated with the error values.

The address Editor looks like this:

public class AddressEditor extends DialogBox implements LeafValueEditor<AddressProxy> {
    ...
    public interface AddressEditorDriver extends RequestFactoryEditorDriver<AddressProxy, AddressEditor> {}
    AddressEditorDriver driver = GWT.create(AddressEditorDriver .class);
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

    private AddressProxy proxy;
    ...
    public void edit(UserContext context, AddresProxy proxy) {
        this.proxy = proxy;
        driver.edit(proxy, context);
    }
    ...
    public void onSaveButtonClick() {
        // this is where the original proxy it's updated
        driver.flush();
        Set<ConstraintViolation<AddressProxy>> constraints = validator.validate(proxy, Default.class);
        if(constraints.size() != 0) {
           Window.alert("Error encountered");
        }
    }
}

So what am I doing wrong? How can i Validate the proxy from the editor, without flushing it to the original proxy ? How can I flush the proxy from driver, only after the modified proxy it's valid ?

役に立ちましたか?

解決

You can't.

Validation needs to operate on the object, so you need to flush the AddressEditorDriver; and because proxies cannot be edited by two request contexts at once, you're forced to use the request context from the other driver, which will give you the same editable proxy as in the other driver, so when you flush the AddressEditorDriver you're modifying the original proxy.

There are workarounds but they all involve a non-negligible amount of work and code:

  • You could clone your proxy (for which there's no easy way to do) before editing it in the AddressEditorDriver and applying the changes back to the original proxy after flush.
  • Maybe you could use an EditorVisitor on your AddressEditorDriver and validate each field as you visit it, so you don't need to flush the driver (which would update the original proxy)
  • Similarly, you could use only sub-editors in the AddressEditor that would validate the field and report the error instead of updating the proxy (i.e. getValue returns the original value from the proxy –the one passed to setValue– rather than the invalid value from the editor)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top