Tapestry5:使用表单验证事件处理多个提交按钮

时间:2012-02-24 13:47:50

标签: tapestry

在Tapestry5中,我在表单中有两个提交按钮,我也想执行验证事件,我该如何实现?这就是我想要做的事情:

在page.tml中

<form t:type="form" t:id="verifyCreateExampleModelForm">

  <input class="btsubmit" t:type="submit" t:id="saveAsAwaitingCompletion" >
  <input class="btsubmit" t:type="submit" t:id="saveAsCreated">
</form>

在page.class中

@OnEvent(value = EventConstants.VALIDATE_FORM, component = "verifyCreateExampleModelForm")
private Object validation() {
    if (StringUtils.isEmpty(modelTypeName)) {
        verifyCreateExampleModelForm.recordError("incorrectmodelTypename"));
        this.isAllowed = false;
    }
}

@OnEvent(component = "saveAsAwaitingCompletion", value = "selected")
private void onSaveAsAwaitingCompletion() {
}

@OnEvent(component = "saveAsCreated", value = "selected")
private void onSaveAsCreated() { 
}

1 个答案:

答案 0 :(得分:8)

正如您所观察到的,selected事件在验证之前发生,因此您无法将操作处理程序代码放入提交按钮的事件处理程序中。但是,您可以在这些方法中存储状态,并在表单事件处理程序中执行实际操作:

@OnEvent(component = "saveAsAwaitingCompletion", value = EventConstants.SELECTED)
void saveAsAwaitingCompletionClicked() {
    this.action = AWAITING_COMPLETION;
}

@OnEvent(component = "saveAsCreated", value = EventConstants.SELECTED)
void saveAsCreatedClicked() { 
    this.action = CREATED;
}

... //validation logic etc.

@OnEvent(component="verifyCreateExampleModelForm" value = EventConstants.SUCCESS)
void save() {
    if (this.action == AWAITING_COMPLETION) {
        ...
    } else {
        ...
    }
}