2012-02-24 32 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 { 
     ... 
    } 
}