问题是删除的 ContactDocumentseditor.getList().remove(index)
仍然通过验证,因此无法保存已编辑的联系人。例如requestContext.save()
,如果我添加新的 ContactDocument 并editor.getList().add()
由于某些字段上的 @NotNull 违规而立即将其删除,则会因 onConstraintViolation 失败。
我有简单的关系:联系人有很多 ContactDocuments。我正在尝试使用请求工厂和编辑器框架编辑联系人。ContactDocuments 使用 JSR-303 进行注释以进行基本验证。
我的 ContactDocuments 集合的 ListEditor:
public class ContactDocumentListEditor extends Composite implements IsEditor<ListEditor<ContactDocumentProxy, ContactDocumentEditor>>, HasRequestContext<List<ContactDocumentProxy>> {
private RequestContext requestContext;
interface ViewUiBinder extends UiBinder<Widget, ContactDocumentListEditor> {}
private final static ViewUiBinder uiBinder = GWT.create(ViewUiBinder.class);
private final ListEditor<ContactDocumentProxy, ContactDocumentEditor> editor = ListEditor.of(new DocumentEditorSource());
@UiField
VerticalPanel container;
@UiField
Button addContactDocumentButton;
@UiHandler("addContactDocumentButton")
void addContactDocumentButtonClick(ClickEvent event) {
addNewContactDocument();
}
public ContactDocumentListEditor() {
initWidget(uiBinder.createAndBindUi(this));
}
private class DocumentEditorSource extends EditorSource<ContactDocumentEditor> {
@Override
public ContactDocumentEditor create(final int index) {
final ContactDocumentEditor documentEditor = new ContactDocumentEditor();
documentEditor.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
removeDocumentEditor(index);
}
});
container.insert(documentEditor, index);
return documentEditor;
}
@Override
public void dispose(ContactDocumentEditor subEditor) {
container.remove(subEditor);
}
@Override
public void setIndex(ContactDocumentEditor subEditor, int index) {
container.insert(subEditor, index);
}
}
@Override
public ListEditor<ContactDocumentProxy, ContactDocumentEditor> asEditor() {
return editor;
}
@Override
public void setRequestContext(RequestContext ctx) {
requestContext = ctx;
}
private void addNewContactDocument() {
ContactDocumentProxy newDocument = requestContext.create(ContactDocumentProxy.class);
editor.getList().add(newDocument);
}
private void removeDocumentEditor(int index) {
editor.getList().remove(index);
}
}
我的演示者来驱动它:
final ContactRequestContext contactRequestContext = contactRequestContextProvider.get();
contactRequestContext.save(contact);
driver.edit(contact, contactRequestContext);
driver.flush().fire(new Receiver<Void>() {
@Override
public void onSuccess(Void response) {
// success
}
@Override
public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
for (ConstraintViolation violation : violations) {
logger.info("Contact constraint violation: " + violation.getPropertyPath() + " = " + violation.getMessage());
}
driver.setConstraintViolations(violations);
}
});
我该如何解决?