2012-02-24 59 views
5

在Tapestry5中,我在表单中有两个提交按钮,我也想执行验证事件,我该如何实现这一点?这就是我要做的: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() { 
} 

回答

4

正如你所观察到的,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 { 
     ... 
    } 
}